Solved

Passing an array into an IN clause in a Stored Proc

Posted on 2012-03-22
7
410 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 500 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

726 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