SQL-delimited string stored procedures

Source: Internet
Author: User
Stored Procedures | Strings I was working on a project to study the technology of querying multiple keywords, which is one of the techniques used.  The role of the "delimited string" stored procedure is to save the string of the "Jiangjs,shenxy,cheng" class to a table. CREATE PROCEDURE [Delimited string]
(
@String NVARCHAR (1000),--string to be delimited
@SPLITCHAR NVARCHAR (10) = ', ',--separator character
@TableName NVARCHAR (50),--Name of the table saved
@FieldName NVARCHAR = ' [ID] '--the field name that is saved
)
As--separates the strings open into the table DECLARE @l INT--the position of the first separator character
DECLARE @S INT--the position of the second separator character
SET @l = 0
SET @S = CHARINDEX (@SPLITCHAR, @String, @l) while @l <= LEN (@String)
BEGIN
DECLARE @ID NVARCHAR () If @S = 0 SET @S = LEN (@String) + 1--if the last string then the position of the second separator character is the length of this string plus a

SET @ID = SUBSTRING (@String, @l, @S-@l)--Take value
SET @l = @S + 1
SET @S = CHARINDEX (@SPLITCHAR, @String, @l) if LTRIM (RTRIM (@ID)) = ' CONTINUE--Skip DECLARE @SQL NVARCHAR (1000) If it is an empty string
SET @SQL = ' INSERT into ' + @TableName + ' (' + @FieldName + ') VALUES (' + @ID + ') '
EXEC sp_executesql @SQL
End
Go----------notmsn messenger:notking@hotmail.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.