-1

I'm trying to create two lists of email addresses, one is a list of all users currently logged in to a database and the other is those who aren't.

I don't seem to be able to populate the @usersin and @usersout variables, I get a NULL output from the final two SELECTs......can anyone cast an eye over my code below and tell me what's wrong with it?

SELECT @maxurn = MAX(ID) FROM ##tmp2
SET @urn = 1
WHILE @urn <= @maxurn

BEGIN

SET @usersin = @usersin + (SELECT [USER_NAME] + ';' FROM ##tmp2 WHERE [LoggedIn]='YES' 
AND ID = @urn FOR XML PATH('')) 

SELECT @urn = @urn + 1
END

SET @urn = 1
WHILE @urn <= @maxurn

BEGIN

SET @usersout = @usersout + (SELECT [USER_NAME] + ';' FROM ##tmp2 WHERE [LoggedIn]='NO'
AND ID = @urn FOR XML PATH('')) 

SELECT @urn = @urn + 1
END

SELECT @usersin
SELECT @usersout
4

1 に答える 1