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