?
Solved

Passing an array into an IN clause in a Stored Proc

Posted on 2012-03-22
7
Medium Priority
?
436 Views
Last Modified: 2012-03-22
Hello Experts.  I have a Stored Procedure that I need to be able to pass a parameter into an IN clause.  

Create Procedure dbo.MyProc

	@ProdCode Varchar(8)
	AS
	
Select	*
From	dbo.Products
Where	ProdCode In (@ProdCode)

----------------------------------------------

Exec dbo.MyProc
@ProdCode = 'LITE,SINK,FAUCET'

Open in new window


From what I understand I may have to create a function to pass this through.  Any direction here is greatly appreciated.
0
Comment
Question by:jobprojn
  • 4
  • 3
7 Comments
 
LVL 33

Accepted Solution

by:
knightEknight earned 2000 total points
ID: 37755665
-- First, create this function:

SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
-- Returns a single-column table from a delimited string
CREATE FUNCTION [dbo].[csv1] (@Source VARCHAR(MAX), @rowDelim VARCHAR(max)=',')
    RETURNS @Result TABLE (ID int identity, col1 varchar(MAX))
    WITH SCHEMABINDING
AS
BEGIN
      IF isNull(@rowDelim,'') in (char(10),'')
      BEGIN
            SELECT @rowDelim = char(13)+char(10)
      END
      ELSE
      BEGIN
            SELECT @Source = replace( @Source, @rowDelim+char(13), @rowDelim )
            SELECT @Source = replace( @Source, @rowDelim+char(10), @rowDelim )
      END

      SELECT @Source = rtrim(ltrim(@Source))
      SELECT @Source = @Source+@rowDelim  WHERE RIGHT(@Source,1) != @rowDelim

      DECLARE @rowDelimPos int=0, @StartShift int=1, @LenSource int=len(@Source)
      WHILE @StartShift <= @LenSource
      BEGIN
            SELECT @rowDelimPos = charindex( @rowDelim, @Source, @StartShift )
            INSERT INTO @Result VALUES ( substring(@Source,@StartShift,@rowDelimPos-@StartShift) )
            --INSERT INTO @Result VALUES ( nullIf(substring(@Source,@StartShift,@rowDelimPos-@StartShift),'') )
            SELECT @StartShift = @rowDelimPos + len(@rowDelim)
      END

      RETURN
END
GO
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 37755671
-- Then, in your procedure you can do this:

Select      *
From      dbo.Products
Where      ProdCode In (select col1 from dbo.csv1(@ProdCode))
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 37755673
-- correction, need to pass in the delimiter to the function:

Where      ProdCode In (select col1 from dbo.csv1(@ProdCode, ',' ))
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

Author Comment

by:jobprojn
ID: 37755693
Thanks for the reply knightEknight.  I ran it and this is only passing the first parameter through the IN clause.  From my example, the query only returns values for LITE.  The SINK and FAUCET values don't get make it in.
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 37755712
please run this test and post the result:

select * from dbo.csv1( 'LITE,SINK,FAUCET', ',' )
0
 

Author Comment

by:jobprojn
ID: 37755745
It returns as rows in a table.

ID      col1
1      LITE
2      SINK
3      FAUCET
0
 

Author Comment

by:jobprojn
ID: 37755805
Got it.  The parameter @ProdCode was set to varchar(8), which was cutting off the string when when the parameter was used in the stored procedure.  Changed it to varchar(2000) and all is good.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question