一个存储过程的问题 求指点
描述: 假设有一张表,8个字段F1, F2, F3, F4, F5, F6, F7, F8。其中F1是表的自增长的关键字,F2是手工输入的多个编号【都是数字】(有两种格式,用"、“分隔或用”-“分隔,如果用”、“分隔,表示并列关系,比如1、3,表示1和3;用”-“分隔,表示从什么到什么,比如1-3,表示,1,2,和3。当然编号数目最多6个),F3到F8默认都是空的.
实现的功能:就是读取每一行,然后把该行的F2的多个编号拆分出来,然后按次序填入F3到F8字段中。
比如第一行是 F1 (1), F2 (5, 7),则填入F3是5,F4是7,F6到F8继续留空
第二行是 F1 (2), F2 (5 - 7),则填入F3是5, F4是6,F5是7, F6到F8继续留空
目前做的T-SQL为:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE SplitData
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
declare @F1 INT
declare @F2 NVARCHAR(50)
declare @F3 TINYINT = NULL
declare @F4 TINYINT = NULL
declare @F5 TINYINT = NULL
declare @F6 TINYINT = NULL
declare @F7 TINYINT = NULL
declare @F8 TINYINT = NULL
declare @TAG NVARCHAR = NULL
declare @COUNT TINYINT = NULL
declare cursor1 cursor for
select F1,F2 from [DB0502].[dbo].[TB0502]
open cursor1
fetch next from cursor1 into @F1,@F2
while @@fetch_status=0
begin
set @F3=SUBSTRING(@F2,1,1);
set @TAG=SUBSTRING(@F2,2,1);
IF @TAG='、'
BEGIN
SET @F4 = SUBSTRING(@F2,3,1);
END
IF @TAG='-'
BEGIN
SET @COUNT = SUBSTRING(@F2,3,1) - @F3;
IF @COUNT = 5
BEGIN
SET @F4 = @F3+1;
SET @F5 = @F4+1;
SET @F6 = @F5+1;
SET @F7 = @F6+1;
SET @F8 = @F7+1;
END
IF @COUNT = 4
BEGIN
SET @F4 = @F3+1;
SET @F5 = @F4+1;
SET @F6 = @F5+1;
SET @F7 = @F6+1;
END
IF @COUNT = 3
BEGIN
SET @F4 = @F3+1;
SET @F5 = @F4+1;
SET @F6 = @F5+1;
END
IF @COUNT = 2
BEGIN
SET @F4 = @F3+1;
SET @F5 = @F4+1;
END
IF @COUNT = 1
BEGIN
SET @F4 = @F3+1;
END
END
UPDATE [DB0502].[dbo].[TB0502] SET F3=@F3,F4=@F4,F5=@F5,F6=@F6,F7=@F7,F8=@F8
where F1=@F1
fetch next from cursor1 into @F1,@F2
end
close cursor1
deallocate cursor1&