Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using parameters in WHERE clouse 1

Status
Not open for further replies.

mnasuto

Technical User
Oct 22, 2001
87
GB
Hi,
I have problem with sp:

DECLARE @U nvarchar(255)
SET @U = 'user_id in (405,255,585)'
SELECT user_id
FROM tbl_user
WHERE [@U]

How can I use parameter or variable with where?

Please help if you can.
Best,
Marta

 

DECLARE @U nvarchar(255)
DECLARE @SQL nvarchar(255)

SET @U = 'user_id in (405,255,585)'
SET @SQL = 'SELECT user_id FROM tbl_user WHERE ' + @U
EXEC(@SQL)

@U must alwas be a string variable, you could write the statement as

SET @U = '405'
SET @SQL = 'SELECT user_id FROM tbl_user WHERE user_id in (' + @U + ')'

but if you pass in an integer variable you will get a 'data type mismatch' error when you try and create the sting variable @STR. You have to convert the integer value to a string variable before you try and make the @SQL

It took me a few frustrated hours to work that out
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top