Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 14th, 2005, 04:23 PM
Authorized User
 
Join Date: Jul 2003
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
Default Sproc not Working with WildCard

Hello Everyone and thanks for your help in advance. I am writing a sproc for a SQL Server 2000 databse and am running into problems. The sproc is effectively a dictionary search looking for wildcard matches. When I write the SQL statement in Query Analyzer, it works fine:

SELECT * FROM tblDiagnosis
WHERE (Diagnosis LIKE '%')

returns all records. However the sproc:

CREATE PROCEDURE [sp_Search]
(@Diagnosis [char](100))
AS
SELECT * From tblDiagnosis
WHERE
([Diagnosis] Like @Diagnosis)
ORDER BY [Diagnosis]
GO

Fails to return any records. However, when I replace the @Diagnosis with '%', it returns all records. Obviously, I want to pass the Diagnosis parameter dynamically so I can perform searches, however, I really don't know why this isn't working. Any help on this issue will be greatly appreciated. Thanks.

 
Old July 14th, 2005, 04:50 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Default

A simple syntax error:

Change as follows

Code:
CREATE PROCEDURE [sp_Search]
(@Diagnosis             [char](100))
AS 
SELECT * From tblDiagnosis 
WHERE 
([Diagnosis]         Like ''' + @Diagnosis + ''')
ORDER BY  [Diagnosis]
Not the three single quotes on each side of @Diagnosis.

Rand
 
Old July 14th, 2005, 04:51 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Not should be Note

Rand
 
Old July 14th, 2005, 05:32 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Oops! I goofed! Try this:

Code:
CREATE PROCEDURE [dbo].[sp_Search]
(@Parameter [varchar](100))
AS
DECLARE @SQL varchar(1000)
SET @SQL = 'SELECT * FROM tblDiagnosis WHERE Diagnosis LIKE (''' + LTRIM(RTRIM(@Parameter)) + ''') ORDER BY Diagnosis'
EXEC(@SQL)
GO

Rand
 
Old July 14th, 2005, 08:08 PM
Authorized User
 
Join Date: Jul 2003
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the help. However, I copied the code directly into the sproc and it didn't work. Still returns nothing. Any ideas?

 
Old July 14th, 2005, 11:28 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

print out the @sql variable before you exectue it and make sure it is properly formatted. If it looks OK, try running that string in QA to see if you get any results.

 
Old July 15th, 2005, 04:40 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Hugh,

Your problem is here:
(@Diagnosis [char](100))

when you pass in '%' as the parameter, then because you set it to a fixed width CHAR, what you are actually passing in is '%+99spaces'. That's why you don't get any matches. Just change the datatype to a varchar and you'll be fine.

rgds
Phil
 
Old July 15th, 2005, 05:38 AM
Authorized User
 
Join Date: Jul 2003
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Phil. That did the trick.






Similar Threads
Thread Thread Starter Forum Replies Last Post
matrix multiplication using wildcard in C/C++ rubnrj C++ Programming 0 March 5th, 2008 03:10 AM
Wildcard on nText field rit01 SQL Server 2000 1 March 17th, 2006 12:48 PM
select wildcard element names groovepapa XSLT 4 September 1st, 2004 08:12 AM
EOF always false with wildcard pacejohn Access VBA 1 August 20th, 2004 12:44 AM
Working with Dates in SPROC [email protected] SQL Language 4 March 10th, 2004 03:56 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.