Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Parameter in Where
Message
 
To
22/04/2013 14:17:35
Gary Foster
Pointsource Consulting LLC
Chanhassen, Minnesota, United States
General information
Forum:
Microsoft SQL Server
Category:
Stored procedures, Triggers, UDFs
Environment versions
SQL Server:
SQL Server 2005
Miscellaneous
Thread ID:
01571626
Message ID:
01571627
Views:
55
>Hi All,
>I'm baffled by what should be a simple thing. I'm trying to use a parameter in a WHERE clause.
>A dirt simple example might be:
>
>declare @Arg varchar(20)
>SET @Arg = 'Joe%'
>
>SELECT * FROM Table WHERE Name LIKE @Arg
>
>I get nothing back from the above while SELECT * FROM Table WHERE Name LIKE 'Joe%' works like a champ.
>
>Clearly, I'm new to doing this. I really want to avoid dynamic SQL here!
>
>Any help is appreciated.
>
>Gary

Your example should work
DECLARE @Test TABLE (Name varchar(200), Field1 int)
INSERT INTO @Test VALUES('Joe Doe',1)
INSERT INTO @Test VALUES('Jane Doe',2)
INSERT INTO @Test VALUES('John Doe',3)
INSERT INTO @Test VALUES('Joe Spncer',4)



declare @Arg varchar(20)
SET @Arg = 'Joe%'

SELECT * FROM @Test WHERE Name LIKE @Arg
Against Stupidity the Gods themselves Contend in Vain - Johann Christoph Friedrich von Schiller
The only thing normal about database guys is their tables.
Previous
Reply
Map
View

Click here to load this message in the networking platform