 |
|
 |
|
Next: Cannot edit SSIS Script Task in SQL Server BIDS 2..
|
| Author |
Message |
External

Since: Jun 04, 2008 Posts: 68
|
(Msg. 1) Posted: Thu Oct 29, 2009 11:33 pm
Post subject: Stored procedure with mulitple items passed in string Archived from groups: microsoft>public>sqlserver>programming (more info?)
|
|
|
I am getting multiple selections from a ListBox and want to send it to a
stored procedure to use in a where clause. Something like:
SELECT Name, Address
FROM Company
WHERE Status in ("Active", "Pending")
I would probably send this some sort of delimited sring.
Would I have to use dynamic sql to handle this?
Thanks,
Tom >> Stay informed about: Stored procedure with mulitple items passed in string |
|
| Back to top |
|
 |  |
External

Since: Jun 01, 2004 Posts: 652
|
(Msg. 2) Posted: Fri Oct 30, 2009 3:25 am
Post subject: Re: Stored procedure with mulitple items passed in string [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
|
|
| Back to top |
|
 |  |
External

Since: Jun 04, 2008 Posts: 68
|
(Msg. 3) Posted: Fri Oct 30, 2009 8:37 am
Post subject: Re: Stored procedure with mulitple items passed in string [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
This might be an idea:
SELECT P.ProductName, P.ProductID
FROM Northwind..Products P
JOIN iter$simple_intlist_to_tbl(@ids) i ON P.ProductID = i.number
Where iter$simple_intlist_to_tbl(@ids) is a function that returns a table.
But what if the string passed were empty or no string passed.
I used to have a dropdown that passed a value or none if "All" were selected
in my dropdown.
So my stored procedure would have @ids defined as:
@ids varchar = null
So I can do the following:
Select...
Where (@ids is null or P.ProductID = @ids)
This would allow nothing to be passed and not be part of the filtering.
Is there a way to do the above Select statement in this way - where nothing
is passed.
Otherwise, it seems that dynamic sql may be the best option.
Thanks,
Tom
"John Bell" wrote in message
>
> "tshad" wrote in message
>
>>I am getting multiple selections from a ListBox and want to send it to a
>>stored procedure to use in a where clause. Something like:
>>
>> SELECT Name, Address
>> FROM Company
>> WHERE Status in ("Active", "Pending")
>>
>> I would probably send this some sort of delimited sring.
>>
>> Would I have to use dynamic sql to handle this?
>>
>> Thanks,
>>
>> Tom
> Tom
>
> If you searched google you would get thousands of examples such as
>
> http://sqlblogcasts.com/blogs/tonyrogerson/archive/2007/03/18/passing-...array-o
>
> and the most comprehensive of articles at
> http://www.sommarskog.se/arrays-in-sql.html
>
> If you are using SQL 2008 then the Table Valued Parameter feature is also
> available.
>
> John >> Stay informed about: Stored procedure with mulitple items passed in string |
|
| Back to top |
|
 |  |
External

Since: Aug 24, 2003 Posts: 1744
|
(Msg. 4) Posted: Sun Nov 01, 2009 5:25 am
Post subject: Re: Stored procedure with mulitple items passed in string [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
Another way
DECLARE @t VARCHAR(50)
SET @t = 'white,smith'
SELECT *
FROM authors
WHERE ',' + @t + ',' LIKE '%,' + au_lname + ',%'
"tshad" wrote in message
> This might be an idea:
>
> SELECT P.ProductName, P.ProductID
> FROM Northwind..Products P
> JOIN iter$simple_intlist_to_tbl(@ids) i ON P.ProductID = i.number
>
> Where iter$simple_intlist_to_tbl(@ids) is a function that returns a table.
>
> But what if the string passed were empty or no string passed.
>
> I used to have a dropdown that passed a value or none if "All" were
> selected in my dropdown.
>
> So my stored procedure would have @ids defined as:
>
> @ids varchar = null
>
> So I can do the following:
>
> Select...
> Where (@ids is null or P.ProductID = @ids)
>
> This would allow nothing to be passed and not be part of the filtering.
>
> Is there a way to do the above Select statement in this way - where
> nothing is passed.
>
> Otherwise, it seems that dynamic sql may be the best option.
>
> Thanks,
>
> Tom
>
> "John Bell" wrote in message
>
>>
>> "tshad" wrote in message
>>
>>>I am getting multiple selections from a ListBox and want to send it to a
>>>stored procedure to use in a where clause. Something like:
>>>
>>> SELECT Name, Address
>>> FROM Company
>>> WHERE Status in ("Active", "Pending")
>>>
>>> I would probably send this some sort of delimited sring.
>>>
>>> Would I have to use dynamic sql to handle this?
>>>
>>> Thanks,
>>>
>>> Tom
>> Tom
>>
>> If you searched google you would get thousands of examples such as
>>
>> http://sqlblogcasts.com/blogs/tonyrogerson/archive/2007/03/18/passing-...array-o
>>
>> and the most comprehensive of articles at
>> http://www.sommarskog.se/arrays-in-sql.html
>>
>> If you are using SQL 2008 then the Table Valued Parameter feature is also
>> available.
>>
>> John
>
> >> Stay informed about: Stored procedure with mulitple items passed in string |
|
| Back to top |
|
 |  |
External

Since: Jan 11, 2008 Posts: 1089
|
(Msg. 5) Posted: Sun Nov 01, 2009 2:25 pm
Post subject: Re: Stored procedure with mulitple items passed in string [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
You used double quotes which made them into data element names and not
strings. THIS IS FUNDAMENTAL!! You are at the point where you cannot
ask a question. Please, please, please, do your homework BEFORE you
come to class.
>> I would probably send this some sort of delimited string. <<
NEVER. Dr. Codd. Foundation of RDBMS. First Normal Form (1NF).
Etc. DUH!
>> Would I have to use dynamic sql [sic: SQL] to handle this? <<
YOU might, but a good SQL programmer would not. All you are getting
here are kludges around FUNDAMENTAL errors (i.e stick a rock under it
and it will look level). I hope that you are not working in a place
where your ignorance can kill people. >> Stay informed about: Stored procedure with mulitple items passed in string |
|
| Back to top |
|
 |  |
External

Since: May 30, 2004 Posts: 2061
|
(Msg. 6) Posted: Sun Nov 01, 2009 7:25 pm
Post subject: Re: Stored procedure with mulitple items passed in string [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
--CELKO-- (jcelko212@earthlink.net) writes:
> You used double quotes which made them into data element names and not
> strings. THIS IS FUNDAMENTAL!! You are at the point where you cannot
> ask a question. Please, please, please, do your homework BEFORE you
> come to class.
>
>>> I would probably send this some sort of delimited string. <<
>
> NEVER. Dr. Codd. Foundation of RDBMS. First Normal Form (1NF).
> Etc. DUH!
>
>>> Would I have to use dynamic sql [sic: SQL] to handle this? <<
>
> YOU might, but a good SQL programmer would not. All you are getting
> here are kludges around FUNDAMENTAL errors (i.e stick a rock under it
> and it will look level). I hope that you are not working in a place
> where your ignorance can kill people.
You stinking a*hole. Learn to behave before you come to class to teach or
whatever you are pretending to do. If you can't answer the question, just
move to the next post, and skip your insults.
--
Erland Sommarskog, SQL Server MVP, esquel.TakeThisOut@sommarskog.se
Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx >> Stay informed about: Stored procedure with mulitple items passed in string |
|
| Back to top |
|
 |  |
External

Since: May 30, 2004 Posts: 2061
|
(Msg. 7) Posted: Sun Nov 01, 2009 7:25 pm
Post subject: Re: Stored procedure with mulitple items passed in string [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
tshad (tfs@dslextreme.com) writes:
> This might be an idea:
>
> SELECT P.ProductName, P.ProductID
> FROM Northwind..Products P
> JOIN iter$simple_intlist_to_tbl(@ids) i ON P.ProductID = i.number
>
> Where iter$simple_intlist_to_tbl(@ids) is a function that returns a table.
>
> But what if the string passed were empty or no string passed.
>
> I used to have a dropdown that passed a value or none if "All" were
> selected in my dropdown.
>
> So my stored procedure would have @ids defined as:
>
> @ids varchar = null
>
> So I can do the following:
>
> Select...
> Where (@ids is null or P.ProductID = @ids)
>
> This would allow nothing to be passed and not be part of the filtering.
>
> Is there a way to do the above Select statement in this way - where
> nothing is passed.
>
> Otherwise, it seems that dynamic sql may be the best option.
SELECT P.ProductName, P.ProductID
FROM Northwind..Products P
WHERE @ids IS NULL OR
EXISTS (SELECT * FROM
iter$simple_intlist_to_tbl(@ids) i
WHERE P.ProductID = i.number)
Or simply:
IF @ids IS NULL
-- plain SELECT
ELSE
-- SELECT like above.
The latter as the advantage of using an index when you specify a list. But
it's not tenable, if you have several options. In that case dynamic SQL
may be way do go. If you haven't looked at my article on
http://www.sommarskog.se/dyn-search-2005.html, you may intereted in
doing so.
--
Erland Sommarskog, SQL Server MVP, esquel RemoveThis @sommarskog.se
Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx >> Stay informed about: Stored procedure with mulitple items passed in string |
|
| Back to top |
|
 |  |
External

Since: Jan 10, 2008 Posts: 213
|
(Msg. 8) Posted: Mon Nov 02, 2009 6:25 am
Post subject: Re: Stored procedure with mulitple items passed in string [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
A true master class of how to wreck the little reputation you had left and
make sure you are never employed by anybody who wants balanced and
un-opinionated advice.
What a complete ignorant troll you are --celko--.
You really are an embarrassment within the industry - is it any wonder
nobody wants you in their work places?
--ROGGIE--
"--CELKO--" wrote in message
> You used double quotes which made them into data element names and not
> strings. THIS IS FUNDAMENTAL!! You are at the point where you cannot
> ask a question. Please, please, please, do your homework BEFORE you
> come to class.
>
>>> I would probably send this some sort of delimited string. <<
>
> NEVER. Dr. Codd. Foundation of RDBMS. First Normal Form (1NF).
> Etc. DUH!
>
>>> Would I have to use dynamic sql [sic: SQL] to handle this? <<
>
> YOU might, but a good SQL programmer would not. All you are getting
> here are kludges around FUNDAMENTAL errors (i.e stick a rock under it
> and it will look level). I hope that you are not working in a place
> where your ignorance can kill people. >> Stay informed about: Stored procedure with mulitple items passed in string |
|
| Back to top |
|
 |  |
| Related Topics: | caveats to using the WHERE "IN" clause with a passed in pa.. - I am having trouble with my attempt to pass in a parameter to a stored procedure in SQL Server 2005. The stored procedure contains a WHERE clause, with the IN keyword specified to use an input parameter, as follows: SELECT ...[snipped]... WHERE..
What does the capital N before the string do in a Stored P.. - Hi --- In the query below, what does the capital N do before the strings? (i.e. name = N'vw_aspnet_MembershipUsers' AND SELECT N'aspnet_Membership') Thanks! IF ((@TablesToCheck & 1) <> 0 AND (EXISTS (SELECT name FROM sysobjects WHERE ...
How do a pass a 'Where' string clause to a stored procedure - I need to be able to construct a SELECT statement in a stored procedure and concatenate a CSV list of values passed as an input parm to the stored procedure. How can I do this without declaring a variable for the Select, then executing it. I need to...
IN clause and string parameters for stored procedure - I have a stored procedure that takes string parameter as input like below @CustomerCode = "'C1', 'C2','C3'" I am trying to use that in SELECT * FROM Customer where CustomerCode IN (@CustomerCode). The Above statement is not working. I kno...
Counting Items in Tables when part string and multivalue - Firstly thank you all for a wonderful site every day I find answers to problems by searching these pages it really is fantastic. My Problem which I have not been able to find an answer for. I have two tables as setup below. Table 1.. |
|
You can post new topics in this forum You can reply to topics in this forum You can edit your posts in this forum You can delete your posts in this forum You can vote in polls in this forum
|
|
|
|
 |
|
|