tags:

views:

37

answers:

3

I am trying to create the following select statement in a stored proc

@dealerids nvarchar(256)

SELECT * 
FROM INVOICES as I
WHERE convert(nvarchar(20), I.DealerID) in (@dealerids)

I.DealerID is an INT in the table. and the Parameter for dealerids would be formatted such as (8820, 8891, 8834)

When I run this with parameters provided I get no rows back. I know these dealerIDs should provided rows as if I do it individually I get back what I expect. I think I am doing

   WHERE convert(nvarchar(20), I.DealerID) in (@dealerids)

incorrectly. Can anyone point out what I am doing wrong here?

+2  A: 

You can't use @dealerids like that, you need to use dynamic SQL, like this:

@dealerids nvarchar(256) 

EXEC('SELECT *  
FROM INVOICES as I 
WHERE convert(nvarchar(20), I.DealerID) in (' + @dealerids + ')'

The downside is that you open yourself up to SQL injection attacks unless you specifically control the data going into @dealerids.

There are better ways to handle this depending on your version of SQL Server, which are documented in this great article.

LittleBobbyTables
Great thanks I will read through that. I appreciate the direction!
twal
+2  A: 

Split @dealerids into a table then JOIN

SELECT * 
FROM INVOICES as I
JOIN
ufnSplit(@dealerids) S ON I.DealerID = S.ParsedIntDealerID

Assorted split functions here (I'd probably a numbers table in this case for a small string

gbn
I tried this, it lets me create the stored proc but when I run it, it throws this expection: Invalid object name 'ufnSplit. unfortunately this in on SqlServer 2000..(i know, they need to get a new version!:))
twal
@twal: read the link, write it. or use KM's answer.
gbn
@twal, for the above code to work, you need to create a function **ufnSplit()** which does the string manipulation and returns a table. There are many links which show various ways to create this function.
KM
+2  A: 

Use a table values parameter (new in SQl Server 2008). Set it up by creating the actual table parameter type:

CREATE TYPE IntTableType AS TABLE (ID INTEGER PRIMARY KEY)

Your procedure would then be:

Create Procedure up_TEST
    @Ids IntTableType READONLY
AS

SELECT * 
    FROM ATable a
    WHERE a.Id IN (SELECT ID FROM @Ids)

RETURN 0
GO

if you can't use table value parameters, see: "Arrays and Lists in SQL Server 2005 and Beyond, When Table Value Parameters Do Not Cut it" by Erland Sommarskog, then there are many ways to split string in SQL Server. This article covers the PROs and CONs of just about every method. in general, you need to create a split function. This is how a split function can be used:

SELECT
    *
    FROM YourTable                               y
    INNER JOIN dbo.yourSplitFunction(@Parameter) s ON y.ID=s.Value

I prefer the number table approach to split a string in TSQL but there are numerous ways to split strings in SQL Server, see the previous link, which explains the PROs and CONs of each.

For the Numbers Table method to work, you need to do this one time table setup, which will create a table Numbers that contains rows from 1 to 10,000:

SELECT TOP 10000 IDENTITY(int,1,1) AS Number
    INTO Numbers
    FROM sys.objects s1
    CROSS JOIN sys.objects s2
ALTER TABLE Numbers ADD CONSTRAINT PK_Numbers PRIMARY KEY CLUSTERED (Number)

Once the Numbers table is set up, create this split function:

CREATE FUNCTION [dbo].[FN_ListToTable]
(
     @SplitOn  char(1)      --REQUIRED, the character to split the @List string on
    ,@List     varchar(8000)--REQUIRED, the list to split apart
)
RETURNS TABLE
AS
RETURN 
(

    ----------------
    --SINGLE QUERY-- --this will not return empty rows
    ----------------
    SELECT
        ListValue
        FROM (SELECT
                  LTRIM(RTRIM(SUBSTRING(List2, number+1, CHARINDEX(@SplitOn, List2, number+1)-number - 1))) AS ListValue
                  FROM (
                           SELECT @SplitOn + @List + @SplitOn AS List2
                       ) AS dt
                      INNER JOIN Numbers n ON n.Number < LEN(dt.List2)
                  WHERE SUBSTRING(List2, number, 1) = @SplitOn
             ) dt2
        WHERE ListValue IS NOT NULL AND ListValue!=''

);
GO 

You can now easily split a CSV string into a table and join on it:

Create Procedure up_TEST
@Ids VARCHAR(MAX)
AS
SELECT * FROM ATable a
WHERE a.Id IN (SELECT ListValue FROM dbo.FN_ListToTable(',',@Ids))
KM