[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.sqlserver.programming

Passing a list/array to stored proc

Stimp

3/15/2007 9:40:00 PM

Hi all,

I'm trying to run a sproc like:

CREATE PROCEDURE spGetGuests

@GuestList varchar(50)

SELECT * FROM Guests
WHERE idGuest IN (' + @GuestList + ')



But I get the following error:

Syntax error converting the varchar value ' + @GuestList + ' to a
column of data type int


idGuest is an int field

GuestList is a string like: 1,24,39,42,52,61


Any ideas on the correct syntax here ?

Thanks!
--

fiddlewidawiddum
7 Answers

--CELKO--

3/15/2007 10:20:00 PM

0

You can Google several different kludges for this; it is a common
newbie misunderstanding. You have some other problems, too. Quit
using those silly prefixes and follow ISO-11179 rules -- example
"spGetGuests" should have been "GetGuests" , and "idGuest" should have
been "guest_id" -- even MS gave up on camel case because it does not
work (your eye jumps to the uppercase letter then backwards tot he
front of the word).

We do not use SELECT * in production code; it is just too dangerous.
I also luck the choice of VARCHAR(50) for a vague, non-scalar variable
-- this is also a magic number newbies use, not the results of
thinking and research.

>> Syntax error converting the varchar value ' + @GuestList + ' to a column of data type int <<

You do not understand that SQL is a **compiled** language and you
expected a string to be used by a BASIC-like interpreter. This is a
much more fundamental error than just no knowing SQL.

You will get a bunch of kludges to split up the string and split it
into integers. They are very slow and dangerous -- "look up SQL
Injection" -- and they do not really work becasue they do not handle
garbage characters, double commas and other things that a real parser
would handle.

Since T-SQL can handle over 1000 parameters, why not declare the
procedure with a long parameter list? You get the benefits of a real
parser so as to avoid the kludges. Try something like this:

CREATE PROCEDURE GetGuests
(@g1 INTEGER, @g2 INTEGER, .., @gn INTEGER)

SELECT guest_id, guest_name, etc.
FROM Guests
WHERE guest_id IN (@g1, @g2, .., @gn);

The default NULLs in the list will be ignored. You might consider
using an EXISTS() or constructing a working table instead, but that is
another issue.

Anith Sen

3/15/2007 11:24:00 PM

0

For a variety of approaches using t-SQL, see:
http://www.projectdmx.com/tsql/sqla...

--
Anith


elaine

3/15/2007 11:48:00 PM

0

On Mar 15, 2:39 pm, Stimp <r...@spumco.com> wrote:
> Hi all,
>
> I'm trying to run a sproc like:
>
> CREATE PROCEDURE spGetGuests
>
> @GuestList varchar(50)
>
> SELECT * FROM Guests
> WHERE idGuest IN (' + @GuestList + ')
>
> But I get the following error:
>
> Syntax error converting the varchar value ' + @GuestList + ' to a
> column of data type int
>
> idGuest is an int field
>
> GuestList is a string like: 1,24,39,42,52,61
>
> Any ideas on the correct syntax here ?
>
> Thanks!
> --
>
> fiddlewidawiddum

You can use dynamic query like this
exec('SELECT * FROM Guests WHERE idGuest IN (' + @GuestList + ')')

Hope this will help.
Elaine

Stimp

3/15/2007 11:52:00 PM

0

On Thu, 15 Mar 2007 Anith Sen <anith@bizdatasolutions.com> wrote:
> For a variety of approaches using t-SQL, see:
> http://www.projectdmx.com/tsql/sqla...

handy.. I'll have a look. Cheers :)

--

fiddlewidawiddum

Stimp

3/16/2007 12:05:00 AM

0

On Thu, 15 Mar 2007 --CELKO-- <jcelko212@earthlink.net> wrote:
> You can Google several different kludges for this; it is a common
> newbie misunderstanding. You have some other problems, too. Quit
> using those silly prefixes and follow ISO-11179 rules -- example
> "spGetGuests" should have been "GetGuests" , and "idGuest" should have
> been "guest_id" -- even MS gave up on camel case because it does not
> work (your eye jumps to the uppercase letter then backwards tot he
> front of the word).
>
> We do not use SELECT * in production code; it is just too dangerous.
> I also luck the choice of VARCHAR(50) for a vague, non-scalar variable
> -- this is also a magic number newbies use, not the results of
> thinking and research.

for some reason my previous post wasn't posted, so maybe my language was
too extreme ;)

Basically this isn't an actual sproc.. I just made something up on the
spot so keep your 'enlightened' comments to yourself mate.

> Since T-SQL can handle over 1000 parameters, why not declare the
> procedure with a long parameter list? You get the benefits of a real
> parser so as to avoid the kludges. Try something like this:
>
> CREATE PROCEDURE GetGuests
> (@g1 INTEGER, @g2 INTEGER, .., @gn INTEGER)
>
> SELECT guest_id, guest_name, etc.
> FROM Guests
> WHERE guest_id IN (@g1, @g2, .., @gn);

that's a horrible horrible solution .. did you really put much 'thinking
and research' into that I wonder

--

fiddlewidawiddum

Parth

3/16/2007 10:55:00 AM

0

Yes! There is some solution for your problem.

Pass the parameter @GuestList as varchar i.e. like: ?1,24,39,42,52,61?
(Use single varchar parameter of proper length as entry list may vary. It is 6 now but may be of 50 or 60 even)

Write a code block for putting comma-separated values in a temp table (for this, use CHARINDEX in a WHILE LOOP to get comma separated values--- Logic will, get the first value, concat it, put in table, make the string length smaller by the number of character it already concated from bingeing of the string, do it in awhile loop till there is no element left in the string )

Say your temp table is T_Temp
so values in T_Table will like below

ID
1
24
39
42
52
61


Then use that table in your query, like below

SELECT * FROM Guests
WHERE idGuest IN (select ID from T_Table )


Thanks and good luck.

-Parth
ghoshps@gmail.com



EggHeadCafe.com - .NET Developer Portal of Choice
http://www.egghe...

Stimp

3/16/2007 1:16:00 PM

0

On Fri, 16 Mar 2007 Parth <Parth> wrote:
> Yes! There is some solution for your problem.

I'll look into that too, thanks!

--

fiddlewidawiddum