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 February 1st, 2006, 09:31 AM
Authorized User
 
Join Date: Dec 2005
Posts: 21
Thanks: 0
Thanked 1 Time in 1 Post
Default Passing an "IN" clause to a stored procedure

I'd like to create a stored procedure where one of the input parameters is the string for an IN clause. In this case the IN clause contains strings, not integers. For example:
Code:
CREATE PROCEDURE getEmployees @InClause varchar(100) AS
SELECT LastName, Salary
FROM Employees
WHERE LastName IN (@InClause)
I would then call the procedure like this:
Code:
execute getEmployees @InClause = "'Anderson','Smith','Jones'"
I can create the stored procedure fine but when I try to execute it, it doesn't return anything even though it should. Am I correct in assuming there's a way to do this without using dynamic SQL?

 
Old February 3rd, 2006, 11:54 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

I am really not sure why your code doesn't work. I would really like to know why. This code does work, althogh I am not sure if it is the best way. Let me know if you get your original code to work, or if you find out an answer as to why it does not work.

WHERE CHARINDEX( LastName , @InClause ) > 0

You can also remove the single quotes when passing in the string.

Jim


 
Old February 4th, 2006, 04:59 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

There are ways to overcome this using a UDF that accepts the comma separated string and returns a table. You can then use this table to JOIN on with your other tables.

I thought there was an example at the Microsoft site, but I can't find it anymore. I found one very similar here: http://www.bigbold.com/snippets/tag/list#post1179

Usage can be as simple as:

SELECT * FROM Foo
     WHERE FooBar IN (SELECT ListItem FROM dbo.fnSplit('Foo','Bar','FooBar'))

Using CharIndex is risky, as it might return unwanted results. For example, if you have a name of John that's matched against Johnson, you still get a result back.

HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old February 4th, 2006, 05:35 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

The IN clause expects a comma delimited list.
You cannot pass a string containing commas.

You can use PATINDEX (or CHARINDEX) but tighten up the boundaries, although performance maybe poor.
Code:
DECLARE @InClause NVARCHAR(100)
SET @InClause = 'tom,dick,harry'
DECLARE @SafeInClause NVARCHAR(100)
SET @SafeInClause = ',' + @InClause + ',' 
SELECT * FROM myTable WHERE PATINDEX(',' + myColumn + ',', @SafeInClause) > 0
--

Joe (Microsoft MVP - XML)
 
Old February 7th, 2006, 02:24 PM
Authorized User
 
Join Date: Dec 2005
Posts: 21
Thanks: 0
Thanked 1 Time in 1 Post
Default

I'm not familiar with UDFs or PATINDEXes. I guess I'll have to look into them. It seems like this is more complicated than it needs to be.

 
Old February 7th, 2006, 10:55 PM
SQLScott's Avatar
Wrox Author
 
Join Date: Dec 2004
Posts: 338
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Here's a table-valued udf example that will do what you need.

DROP FUNCTION udf_SplitIDString;
GO

create function udf_SplitIDString
(
  @IDString varchar(1000)
)

returns @IDs table
(ContactID int)
as

begin
 declare @Position int
 while len(@IDString) > 0
   begin
     set @Position = charindex(',', @IDString)
     if @Position > 0
       begin
         insert @IDs
         select convert(int, left(@IDString, @Position - 1))
         set @IDString = right(@IDString, len(@IDString) - Position)
       end
     else
       begin
         insert @IDs
         select convert(int, @IDString)
         set @IDString = ''
       end
   end
return
end

DECLARE @userids varchar(2000)
SET @userids = '1, 2, 3, 4, 5'
SELECT a.*
FROM Person.Contact a
inner join udf_SplitIDString(@userIDs) b
on a.ContactID = b.ContactID








Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing Paramter-Stored Procedure ms1234 Crystal Reports 2 May 22nd, 2007 11:34 AM
Passing datasource to a stored procedure ctranjith SQL Server 2000 2 October 7th, 2004 01:49 PM
Passing a parameter value to Stored Procedure mcinar SQL Server 2000 9 October 3rd, 2004 09:42 PM
passing stored procedure shoakat SQL Server 2000 1 July 15th, 2004 09:20 AM
Stored Procedure - Dynamic Where Clause Terry_Pino BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 1 July 2nd, 2004 04:39 PM





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