Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Passing an array into an IN clause in a Stored Proc

Posted on 2012-03-22
7
409 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
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…

807 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