Coalesce to a comma value then used in an IN Query


I am using the Coalesce function to combine records into a comma variable (I'm using SQL 2012) which is then used in another query for the IN query, this is the stored proc.





-- List all current approvers into a comma value for the IN command
SELECT @listStr = COALESCE(@listStr+',' ,'') + LoginID
FROM tbl_Approvers

-- Set a dummy value if the returned list is empty
IF (@listStr IS NULL)
   SET @listStr = '-1' 

-- Get available users 
SELECT * FROM [tbl_login] WHERE ([BldID] = @BldID AND UserRole = 'Admin') AND NOT (Id IN (@listStr))

Open in new window

However, this works when only one row in the DB but fails if there are several rows with the following message.

Msg 245, Level 16, State 1, Procedure spTPListApproversAvailable, Line 23
Conversion failed when converting the varchar value '1016,' to data type int.

Open in new window

Any help is appreciated.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongSoftware Team LeadCommented:
try change:

SELECT @listStr = COALESCE(@listStr+',' ,'') + LoginID

Open in new window


SELECT @listStr = CONCAT( COALESCE(@listStr+',' ,'') , LoginID )

Open in new window

sanjshah12Author Commented:
Thanks Ryan for responding, I tried this the line but the error is still the same
AneeshDatabase ConsultantCommented:
you need dynamic SQL  for this

EXEC( 'SELECT * FROM [tbl_login] WHERE ([BldID] = @BldID AND UserRole = ''Admin'') AND NOT (Id IN ('+@listStr+')) )
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

sanjshah12Author Commented:
Thanks Aneesh for responding, I tried this (added the missing ') but still receive the same error message
AneeshDatabase ConsultantCommented:
try printing the value

Print 'SELECT * FROM [tbl_login] WHERE ([BldID] = @BldID AND UserRole = ''Admin'') AND NOT (Id IN ('+@listStr+'))
AneeshDatabase ConsultantCommented:
here is another option

SELECT * FROM [tbl_login] WHERE ([BldID] = @BldID AND UserRole = 'Admin') AND NOT EXISTS (SELECT 1 FROM tbl_Approvers WHERE BldID = @BldID and ID= LoginID )

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Scott PletcherSenior DBACommented:
Don't create a character list of ids at all.  That's much less efficient than just doing the table lookup in the main query anyway:

-- Get available users
FROM [tbl_login] tl
WHERE ([BldID] = @BldID AND UserRole = 'Admin') AND
    NOT EXISTS(SELECT 1 FROM tbl_Approvers a WHERE a.BldID = @BldID AND a.LoginID = tl.ID)
sanjshah12Author Commented:
Many thanks lads, I never came across this option before !
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.