Stored procedure splits fields to form tables

First, the split stored procedure

 

ALTER FUNCTION [dbo].[GLXSDeal_F](@guid VARCHAR(50),@str varchar(1000),@flag VARCHAR(10))

Returns @tableName Table

(

taskGuid VARCHAR(50),

str2table varchar(50)

)

As

--This function is used to turn a comma-separated data string into a column of a table, for example, the string '1,2,3,4,5' will program a table, this table

Begin

set @str = @str+@flag

Declare @insertStr varchar(50) -- the first string after interception

Declare @newstr varchar(1000) -- the remaining string after intercepting the first string

set @insertStr = left(@str,charindex(@flag,@str)-1)

set @newstr = stuff(@str,1,charindex(@flag,@str),'')

Insert @tableName Values(@guid,@insertStr)

while(len(@newstr)>0)

begin

   set @insertStr = left(@newstr,charindex(@flag,@newstr)-1)

   Insert @tableName Values(@guid,@insertStr)

   set @newstr = stuff(@newstr,1,charindex(@flag,@newstr),'')

end

Return

End

 

 

 

2. Call the stored procedure of the split process, you can pass parameters, and split the string according to the passed parameters

ALTER PROCEDURE [dbo].[StrToTable]

 

    

As

--This function is used to turn a comma-separated data string into a column of a table, for example, the string '1,2,3,4,5' will program a table, this table

IF EXISTS  (SELECT  * FROM dbo.SysObjects WHERE ID = object_id(N'[#TmpGLXS]') AND OBJECTPROPERTY(ID, 'IsTable') = 1) 

drop table #TmpGLXS

 

CREATE Table #TmpGLXS 

(

   taskGuid VARCHAR(32),

str2table varchar(50)

)

 

 

Begin

 

Declare @CTLGuid varchar(1000) 

Declare @str varchar(20) 

Declare Cur Cursor For 

 

SELECT CTLGuid,IGLXS FROM dbo.TaskLineBudget 

Open Cur 

Fetch next From Cur Into @CTLGuid,@str 

While @@fetch_status=0 

BEGIN

 

 

INSERT INTO #TmpGLXS(taskGuid,str2table)  

--flag is ','

SELECT * FROM [dbo].[GLXSDeal_F](@CTLGuid,@str,',')

 

Fetch next From Cur Into @CTLGuid,@str 

End

Close Cur 

Deallocate Cur 

 

SELECT * FROM #TmpGLXS

End

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326801223&siteId=291194637