Microsoft SQL Server
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMicrosoft SQL Server

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 22nd, 2003, 08:33 AM
obi-dad obi-dad is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 6 obi-dad User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
paramter for 'where ... in ... '

When I hardcode a value for the following code, it works, but when I try to pass a paramter of the same value is doesn't return any rows:

ALTER PROCEDURE GetVendorsByList
(
@VendorList varchar(10)
)
AS
SELECT VendorID
FROM Vendors
WHERE VendorID in (' & @VendorList & ')

I eventually want to be able to pass in a list like '1','2','3', but to test I am only passing in '2'.

This is probably a stupid error, what am I doing incorrectly?

Reply With Quote
  #2  
Old April 22nd, 2003, 06:37 PM
stumpy's Avatar
stumpy stumpy is offline
May contain nuts.
Dev Articles Regular (2000 - 2499 posts)
 
Join Date: Aug 2002
Location: Sydney, AU
Posts: 2,058 stumpy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 8 m 57 sec
Reputation Power: 9
Send a message via ICQ to stumpy Send a message via MSN to stumpy
Are you supposed to have quotes around your parsed variable when use stored procedures? (I've just started using them, so am not all that cluey.)

Also, I'm being picky, but why is your ID field a varchar? Should be an int
__________________
DevArticles Moderator
BlueSix - Web Development and Consulting

Reply With Quote
  #3  
Old April 22nd, 2003, 09:22 PM
obi-dad obi-dad is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 6 obi-dad User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I started out with the ID field as int, but realized that a list of vendors such as '1234567', '1234568', '1234569',.....'123456666', doesn't fit into in int field.

The problem is that just 2 or '2' doesn't work, let alone a list.

Reply With Quote
  #4  
Old April 25th, 2003, 11:08 AM
numbernine numbernine is offline
Up To His Eyes In Ads
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: Chicago
Posts: 160 numbernine User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 m 25 sec
Reputation Power: 6
Stumpy's right. What you're doing right now is asking for all the records that compare to the literal string (" & @VendorList & "). To do this the way you want to, I think you would need to build an SQL string dynamically and execute it.

Code:
ALTER PROC GetVendorsByList
	(
	@arrayVendors nvarchar(50)
	)
AS


     SET NOCOUNT ON
     DECLARE @nsql nvarchar(4000)
     SET @nsql = '
         SELECT VendorID
         FROM [Vendors]
         WHERE [VendorID] IN ( ' + @array + ')'

     EXEC sp_executeSQL @nsql


You would call it like:

Code:
EXEC sp_GetVendorsByList '''1'',''2'',''3'''


I would think about taking a second look at your database structure and programming logic that leads you to a situation where you have a collection of VendorIDs and no idea which ones are valid.

Reply With Quote
  #5  
Old April 25th, 2003, 12:18 PM
obi-dad obi-dad is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 6 obi-dad User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
hmm.. how is the sql statement with the 'where ... in ..' that you have:
SELECT VendorID
FROM [Vendors]
WHERE [VendorID] IN ( ' + @array + ')'

different from the one I have:

SELECT VendorID
FROM Vendors
WHERE VendorID in (' & @VendorList & ')

The only difference I see the the brackets surrounding the table name.

What I am doing prior to this call is building a list of vendors that a customer wants to process later. So I am storing a collection of vendorIDs, and in this call I want to retrieve data from each vendor in the collection. Since this is web-based I do not want to store all the data from each vendor while they are selecting vendors, only after all are selected.

How would you go about doing this since it seems my approach is not a good one?

Reply With Quote
  #6  
Old April 25th, 2003, 12:36 PM
numbernine numbernine is offline
Up To His Eyes In Ads
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: Chicago
Posts: 160 numbernine User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 m 25 sec
Reputation Power: 6
Quote:
The only difference I see the the brackets surrounding the table name.

Give it another look. It's building the SQL into a string that includes the array you specify that is then executed. This works on my database.

As for the data structure, that does sound like a reasonable way to accomplish it.

Reply With Quote
  #7  
Old April 25th, 2003, 12:41 PM
obi-dad obi-dad is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 6 obi-dad User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
ah, I see. what does 'sp_executeSQL' refer to? I am using SQLServer if that makes a difference.

Also, I have only done rudimentary stored procedures, do you know any online sites that go deeper into stored procedures than the basic tutorials?

Reply With Quote
  #8  
Old April 25th, 2003, 12:52 PM
numbernine numbernine is offline
Up To His Eyes In Ads
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: Chicago
Posts: 160 numbernine User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 m 25 sec
Reputation Power: 6
Did it work?

SQL Server has a series prewritten stored procedures called "system" stored procedures that you can call in your own programming--they're kind of like built in functions in ASP.

'sp_executeSQL' just takes a string and executes it as SQL.

The problem with your Stored Procedure wasn't anything wrong with the programming; it was in the way SQL Server evaluates Stored Procedure parameters. SQL Server always evaluates them as literal strings. So if you want SQL Server to look at part of the parameter as a literal string and part of it as a command (like the comma separating values) you have to build the string by hand and then execute it.

This is a great book on Transact-SQL:
http://www.amazon.com/exec/obidos/t...=books&n=507846

Does anyone know of any good more indepth online tutorials?

Reply With Quote
  #9  
Old April 25th, 2003, 03:53 PM
obi-dad obi-dad is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 6 obi-dad User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
numbernine you are the man! It worked fine. And I ordered the book

Reply With Quote
  #10  
Old April 25th, 2003, 03:56 PM
obi-dad obi-dad is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 6 obi-dad User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
numbernine, I was using the expression 'you are the man' in its genderless meaning in case you are female. Should have checked first before using that one.

Reply With Quote
  #11  
Old April 25th, 2003, 03:57 PM
numbernine numbernine is offline
Up To His Eyes In Ads
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: Chicago
Posts: 160 numbernine User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 m 25 sec
Reputation Power: 6
LOL--Very sensitive-minded of you to catch that, but yes I am a man...

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMicrosoft SQL Server > paramter for 'where ... in ... '


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway