My guess is that id
is an integer, and you are comparing it to a string. By the rules of SQL Server type conversions, the comparison is done as an integer, not a string, and you get an error. Even if the comparison were done as a string, it still would not be what you want.
So, a correct version would be:
ALTER PROCEDURE [dbo].[sp_UpdateBulkPageStatus]
@Id int,
@Status bit
AS
BEGIN
UPDATE Page SET Status=@Status WHERE Id = @id
END;
That is probably not what you really want the stored procedure to do, because it would only take one id.
There are numerous ways to pass multiple values into a stored procedure. One method is using like
:
WHERE ',' + @ids + ',' like '%,' + cast(Id as varchar(255)) + ',%'
Another method is to find a split()
function on the web, and do:
WITH ids as (
SELEcT id
FROM dbo.split(@ids, ',') as s(id)
)
UPDATE Page p
SET Status = @Status
WHERE p.Id in (SELECT ids.id FROM ids) ;
There are other methods for passing a list into a stored procedure, such as:
- Using a
while
loop and string manipulation.
- Passing in a table parameters.
- Using a global temporary table.
- Writing dynamic SQL.
The use of in
with a string variable is not one of them.
Erland Sommarskog has a rather complete discussion on this topic. If you want to understand different approaches, then read this. (Note: there might be some newer functionality in SQL Server 2014/2016 not covered by this blog.)