SQL stored procedures Update data with multiple IDs

Source: Internet
Author: User

Here is an example to illustrate:

I want to add the name of a part of the brand (that is, brand_name) followed by 1,brand_id is the primary key, the SQL statement is easy to implement, but how does the stored procedure write?

The following error is spelled:

//************************************************

ALTER PROC [dbo]. [Stored procedure name]

@Brand_IDs varchar (max)

As
BEGIN

UPDATE T_system_brand

SET

brand_name=brand_name+ ' 1 '

WHERE brand_id in (@Brand_IDs)

END

//**************************************************

Correct wording (in two steps):

The first step:

programmable line-and function-and table-valued functions

ALTER FUNCTION [dbo]. [Split]
(
@c VARCHAR (MAX),
@split VARCHAR (50)
)
RETURNS @t TABLE (Col VARCHAR (50))
As
BEGIN
while (CHARINDEX (@split, @c) <> 0)
BEGIN
INSERT @t (COL)
VALUES (SUBSTRING (@c, 1, CHARINDEX (@split, @c)-1))
SET @c = STUFF (@c, 1, CHARINDEX (@split, @c), ")
END
INSERT @t (col) VALUES (@c)
RETURN
END


Step Two:

This can be written in a stored procedure:

ALTER PROC [dbo]. [Stored procedure name]

@Brand_IDs varchar (max)

As
BEGIN

UPDATE T_system_brand

SET

brand_name=brand_name+ ' 1 '

WHERE brand_id in ( SELECT * from Split (@Brand_IDs, ', '))

END


Note: I put the table-valued function:





SQL stored procedures Update data with multiple IDs

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.