如果我想编写如下的程序,还有其他一些方法, 为了避免使用连接SQL语句,我只是害怕,如果输入太长,超过max varchar的限制,代码就会有很大的问题。
由于
CREATE PROCEDURE UPDATE_ALL_STATUS
@IDs varchar(MAX) = null,
@status int = null
AS
BEGIN
IF @IDs is null
BEGIN
RETURN
END
DECLARE @SQL VARCHAR(MAX)
SET @SQL = 'UPDATE mytable SET status = ' + @status + ' WHERE id in (' + @IDs + ')'
EXECUTE @SQL
END
答案 0 :(得分:4)
-- Creates the TVP type - only needed once!
CREATE TYPE IntegerTableType AS TABLE
( Identities INT );
GO
CREATE PROCEDURE UPDATE_ALL_STATUS
@IDs IntegerTableType READONLY,
@status int = null
AS
BEGIN
UPDATE mytable
SET status = @status
WHERE id IN
(SELECT Identities FROM @IDs)
END
This MSDN article显示了如何从.NET代码中调用它们。