Unix Technical Forum

Wildcards in SQL Server stored procedures

This is a discussion on Wildcards in SQL Server stored procedures within the SQL Server forums, part of the Microsoft SQL Server category; --> I thought this problem would go away over the Christmas holiday, but of course it did not. I'm trying ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 07:24 PM
Ralph Noble
 
Posts: n/a
Default Wildcards in SQL Server stored procedures

I thought this problem would go away over the Christmas holiday, but
of course it did not. I'm trying to write a stored procedure
incorporating wildcards, so I can search for variations. Example, if
name 'Smith' is submitted, sproc should retrieve all records
containing 'John Smith', 'Zenia Smith', 'Smithfield & Co.' You get the
idea.

Using SQL Query Analyzer, the query

select * from file
where name like '%smith%'

works like a charm.

But if I write a stored procedure declaring the variable @name and
using a where clause 'where name like '%@name%'', I get zero results.
The query doesn't bomb. It just doesn't produce anything - even though
I know there are records that meet the criteria.

Any ideas? Or are sprocs and wildcards incompatible?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 07:24 PM
Erland Sommarskog
 
Posts: n/a
Default Re: Wildcards in SQL Server stored procedures

Ralph Noble (ralph_noble@hotmail.com) writes:
> But if I write a stored procedure declaring the variable @name and
> using a where clause 'where name like '%@name%'', I get zero results.
> The query doesn't bomb. It just doesn't produce anything - even though
> I know there are records that meet the criteria.


Insert someone called John @nameson (yes, with at-sign and all) and you will
get a result.

Unlike languages like Perl or Unix Shells there is interpolation of
variables in T-SQL, so you need to write:

LIKE '%' + @name + '%'


--
Erland Sommarskog, SQL Server MVP, sommar@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:52 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com