I'm wondering why these two following conditions in SQL return different results
First Condition
I write query as following :
DECLARE @idUser VARCHAR(MAX)
SELECT @idUser = COALESCE(@idUser+', ' , '') + CAST(idUser as VARCHAR(MAX))
FROM webuser
WHERE username IN ('John', 'Tom')
And it returns the user id for each usernames, below is the output given
1020, 1021
Second Condition
I write another different query. The username is already set as a parameter, and this is actually what I need, the username is set as parameter, below is the query used :
DECLARE @username VARCHAR(MAX)
SET @username = '''John'', ''Tom'''
DECLARE @idUser VARCHAR(MAX)
SELECT @idUser = COALESCE(@idUser+', ' , '') + CAST(idUser as VARCHAR(MAX))
FROM webuser
WHERE username IN (@username)
And it returns NULL
value as the result.
I'm expecting the two queries return same output, I'm thinking that it's something about the quote in the @username
, did I do it wrong or do you guys have any explanation and solution for this case ?