Copy Code code as follows:
Use Demo
Go
/*
Extract the value from the column string of the table code into the record table
The character type in String is
dsddddd,2222222,222221,3
The last one was labeled for Biaoji in the record table
The value corresponding to the record table is divided by ', ' in the preceding
*/
Go
DROP PROC Proc_split_code
Go
CREATE PROC Proc_split_code
As
BEGIN
SET NOCOUNT on
DECLARE @Count INT--Number of bars
DECLARE @Index INT--Variable
SET @Index = 1--Default
SELECT @Count = Count (*) from Code
--print @Count
while (@Index <= @Count)
BEGIN
DECLARE @BiaoJi INT--Mark
DECLARE @String NVARCHAR (1000)--string
DECLARE @Temp INT--the position of the separator
DECLARE @Star INT--Start position
DECLARE @Code NVARCHAR (100)--
SET @Star = 0
SELECT @String =reverse (String)
From (
SELECT row_number () over (order by String) as rownumber,* from Code
) as a
WHERE RowNumber between @Index and @Index
SET @Temp =charindex (', ', @String, @Star)
SET @BiaoJi = SUBSTRING (@String, @Star, @Temp)
PRINT @BiaoJi
SET @String = REVERSE (@String)
SET @Temp =charindex (', ', @String, @Star)
SET @Star = 0
while (@Temp >0)
BEGIN
SET @Temp =charindex (', ', @String, @Star)
--print @Star
--print @Temp
IF @Temp >0
BEGIN
SET @Code =substring (@String, @Star, @Temp-@Star)
PRINT @Code
--INSERT into the appropriate table
INSERT into record (biaoji,value,time)
VALUES (@BiaoJi, @Code, GETDATE ())
End
SET @Star = @Temp +1
End
--print @Index
PRINT @String
SET @Index = @Index +1
End
End
Go
EXEC Proc_split_code