SQLServer SSP

zhaozj2021-02-16  54

The "Separated string" stored procedure is to save the string of "jiangjs, shenxy, cheng" class to a table.

Create Procedure [Separate string]

(

@String nvarchar (1000), - Splitted strings

@Splitchar nvarchar (10) = ',', - Deliberate

@Tablename nvarchar (50), - Deposit Table Name

@Fieldname nvarchar (50) = '[id]' - stored in the field name

)

AS

- Separate strings in the open input

Declare @l int - the location of the first divided character

Declare @s int - the second separator position

Set @L = 0

Set @S = charindex (@splitchar, @string, @L)

While @L <= len (@string)

Begin

Declare @id nvarchar (50)

IF @S = 0 set @s = len (@string) 1 - If the last string is then the position of the second divided character is the length of this string plus one

Set @id = substring (@String, @L, @S - @L) - Value

Set @L = @S 1

Set @S = charindex (@splitchar, @string, @L)

IF LTRIM (RTRIM (@ID)) = '' Continue - Skip if it is an empty string

Declare @sql nvarchar (1000)

Set @SQL = 'INSERT INTO' @tablename '(' @fieldname ') VALUES (' '' @ID '') '

EXEC SP_EXECUTESQL @SQL

End

Go

------------

转载请注明原文地址:https://www.9cbs.com/read-18063.html

New Post(0)