Solved

Passing an array into an IN clause in a Stored Proc

Posted on 2012-03-22
7
400 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
encyps queries mssql 15 27
backups - Strategies 1 13
Safely Uninstall SQL Server 2008 R2 Express 3 26
Stored Procedure 2 10
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now