0

I have the following procedure:

ALTER PROCEDURE [dbo].[UpdateAllClients] @ClientIDs varchar(max) AS BEGIN SET NOCOUNT ON DECLARE @vSQL varchar(max) SET @vSQL = 'UPDATE Clients SET LastOnline=GETDATE() WHERE ClientID IN (' + @ClientIDs + ')'; EXEC(@vSQL); END 

The @ClientIDs contains an array of many Client ID's, I want to update another field in the Clients table, so I want the query to look like:

UPDATE Clients SET LastOnline=GETDATE(), Status='Open' WHERE ClientID IN (@ClientIDs) 

The problem is that I can put a quotation mark inside the @vSQL variable.

Any clue how can I add the ", Status='Open'" to my query?

2 Answers 2

4

Use two quotes:

SET @vSQL = 'UPDATE Clients SET LastOnline=GETDATE(), Status=''Open'' WHERE ClientID IN (' + @ClientIDs + ')'; 
Sign up to request clarification or add additional context in comments.

Comments

2

I would suggest passing the client IDs as a table valued parameter, rather than a string. Then you can just write SQL, instead of dynamic SQL.

1 Comment

+1 - yes that's exactly what should be done! It is so much more efficient and it doesn't suffer from any of dynamic SQL's typical issues (that is, apart from injection and formatting issues, it also doesn't have a size restriction of 8kb for the query text size).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.