SQL inserts the field values separated by commas (,) into the table cyclically.

Source: Internet
Author: User
Insert the field values separated by a single character into the table cyclically
Declare @ strchatrooms nvarchar (1000) -- custom parameter, value containing the ',' symbol
Declare @ userid int -- custom Parameter
Declare @ tmpnum int -- check ',' character, move position
Declare @ tmpid int -- get the separated values for inserting the table
Declare @ isuccess int -- a viewing error is returned.

Begin tran
-- Case string
Set @ strchatrooms = '11, 12,13, 14,15 ,'
-- Initialization check ',' symbol position, symbol custom
Set @ tmpnum = 1
-- Inserts data separated by commas (,) into the information table cyclically.
While Len (@ strchatrooms)> 0
Begin
If substring (@ strchatrooms, @ tmpnum, 1) = ','
Begin
Set @ tmpid = cast (left (@ strchatrooms, @ tmpNum-1) as INT)
Set @ strchatrooms = right (@ strchatrooms, Len (@ strchatrooms)-@ tmpnum)
Set @ tmpnum = 1
Insert into chatadmin (userid, chatroomid) values (@ userid, @ tmpid)
If @ error <> 0
Begin
Rollback tran
Set @ isuccess =-14
Return
End
End
Else
Set @ tmpnum = @ tmpnum + 1
End
Commit tran

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.