Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / DB Engine / SQL Server / March 2008

Tip: Looking for answers? Try searching our database.

Problem with LIKE in stored procedure

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
GaryDean - 12 Mar 2008 01:39 GMT
I Can't seem to get LIKE to work in a stored procedure.  For instance this
sql works in a query...

SELECT     username, approle, billingcustomer, emailredundant, groupadmin,
branch, nakey, companyadmin, bcstring, dateapproved
FROM         users
WHERE     (branch LIKE '%%') AND (bcstring LIKE '%%') AND (dateapproved IS
NULL)
ORDER BY username

But this stored procedure returns no records when %% is supplied in the two
argumants...

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[GetUnapprovedUsers]
(
@branch nchar(4),
@bcstring nvarchar(20)
)
AS
SET NOCOUNT ON;
SELECT     username, approle, billingcustomer, emailredundant, groupadmin,
branch, nakey, companyadmin, bcstring, dateapproved
FROM         users
WHERE     (branch LIKE @branch) AND (bcstring LIKE @bcstring) AND
(dateapproved IS NULL)
ORDER BY username

Anyone know why?
Thanks,
Gary
Dan Guzman - 12 Mar 2008 02:11 GMT
> @branch nchar(4),

Note that nchar is fixed length so:

GetUnapprovedUsers
   @branch  = N'%%'
   @bcstring = N'%%'

Is equivalent to:

GetUnapprovedUsers
   @branch  = N'%%  '
   @bcstring = N'%%'

You won't get any matches unless you have branches with spaces.  I'm not
sure I understand why you specify 2 wildcard characters.

Signature

Hope this helps.

Dan Guzman
SQL Server MVP
http://weblogs.sqlteam.com/dang/

>I Can't seem to get LIKE to work in a stored procedure.  For instance this
>sql works in a query...
[quoted text clipped - 30 lines]
> Thanks,
> Gary
GaryDean - 12 Mar 2008 16:43 GMT
Dan,
you are not making sense to me.  % means "any string of zero or more
characters".  WHERE branch LIKE '%%' is equivilent to not having the WHERE
clause at all.  % is not a "wild card character."  My example stored
procedure and plain sql are totally equivilent - one works the other does
not.
Gary

>> @branch nchar(4),
>
[quoted text clipped - 47 lines]
>> Thanks,
>> Gary
Dan Guzman - 13 Mar 2008 03:07 GMT
> My example stored procedure and plain sql are totally equivilent - one
> works the other does not.

These are not equivalent and that is why you get different results.  Let me
try to explain another way.

The select statement works because you are passing only wildcards (exactly 2
percent signs).  This is almost the same as no WHERE clause except that NULL
values will be excluded.

The stored procedure is different because the @branch parameter is declared
as fixed length of 4.  When you pass 2 percent signs, the actual value used
in the LIKE expression is the 2 percent signs plus 2 blanks ('%%  ').  This
means that you will only find branches that end in 2 blanks.  I think you
will get results you expect if you declare the parameter as nvarchar(4)
instead of nchar(4).

As I mentioned earlier, although 2 leading percent signs will work, the
second is superfluous.

Signature

Hope this helps.

Dan Guzman
SQL Server MVP
http://weblogs.sqlteam.com/dang/

> Dan,
> you are not making sense to me.  % means "any string of zero or more
[quoted text clipped - 55 lines]
>>> Thanks,
>>> Gary
GaryDean - 13 Mar 2008 16:23 GMT
Yes, that was the problem.  Thanks for the help.
Gary

www.deanblakely.com
>> My example stored procedure and plain sql are totally equivilent - one
>> works the other does not.
[quoted text clipped - 75 lines]
>>>> Thanks,
>>>> Gary
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.