all groups > sql server misc > march 2005 >
You're in the

sql server misc

group:

Converting delimited varchar @parameter for use in NOT IN()


Converting delimited varchar @parameter for use in NOT IN() Patrick Russell
3/7/2005 8:12:57 PM
sql server misc:
I am creating a stored procedure which is passed a comma delimited string of
ids as a varchar datatype. The param is to be used in an SQL statement such
as:

CREATE PROCEDURE GetFromTable
@IDs varchar(255)
AS

SELECT * FROM table WHERE iId NOT IN(@IDs)

GO

The problem is that the iId field is of datatype int, so i get an error
converting the varchar datatype @IDs to int.

I can not use dynamic SQL as i am not able to give table level access. It
has to be via EXEC rights on the stored procedure.

Any Help?

Thanks

Patrick

Re: Converting delimited varchar @parameter for use in NOT IN() bd
3/8/2005 4:16:08 AM
Hi Patrick.
You could write a function like...

-- pseudo code
create function udtSplitIDs( @ids varchar(1000) )
returns @IDsTable table
(
id int
)
as
begin
while (get position of comma)
begin
insert @IDsTable values( @strValue )
find next comma
end

return @IDsTable
end

your select could then be:

SELECT * FROM table
WHERE iId NOT IN(SELECT * FROM udtSplitIDs(@IDs))

Bryce
Re: Converting delimited varchar @parameter for use in NOT IN() recoil NO[at]SPAM community.nospam
3/8/2005 9:21:34 AM
Out of curiousity. A query like that should be avoided if possible in a
high-performance situation due to performance issues, I assume?
Re: Converting delimited varchar @parameter for use in NOT IN() Ray
3/8/2005 12:09:53 PM
Patrick,

Parse the @IDs into rows of a temp table or table variable then use a join
or a subselect. The in operator will not take a variable like this without
building dynamic SQL.


[quoted text, click to view]

AddThis Social Bookmark Button