Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Parameterized Queries

Author  Topic 

StacyOW
Yak Posting Veteran

93 Posts

Posted - 2012-08-08 : 11:40:35
I am making a sql parameterized query in visual Studio 2010 and I am using the query designer. I am wondering how to send multiple values to the query. For instance, for the Status field in the table the user wants to get "Open or Closed" items. How can I send that to the query through a parameter. The parameter is @StatusValue.

I've tried Open or Closed, Open and Closed. Not sure how to send two values for one field in one parameter.

Thank you for your help!

Stacy

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-08 : 12:00:12
use a multi valued parameter and for your query behind use string parsing logic to get individual values out and filter using it

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

StacyOW
Yak Posting Veteran

93 Posts

Posted - 2012-08-09 : 10:59:20
Ok if I use the multi valued parameter does it always have to be multi then, because the @StatusValue may sometimes be just one value in the parameter.
Here is what I am doing. It is a search form for the user. They can choose value to search by so if they choose Vendor then they can also choose whether they want Open, Closed or Both for the status field. So of course it works just fine if they choose either Open or Closed but if I want it to bring up both Open and Closed I wasn't sure how to do that. Basically I need the parameter to except 1 value or many values. Is that possible? Can you give me an idea where to find directions for doing such a thing.
Thanks so much for your reply.
Stacy (Newbie)
P.S. this is for a fillby query on a winform not for a report. Or is there a way to send something in the parameter that would allow All values in the field, because that is basically what I am doing. They choose open or closed or I want everything. Which way is easier?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-09 : 11:23:56
quote:
Originally posted by StacyOW

Ok if I use the multi valued parameter does it always have to be multi then, because the @StatusValue may sometimes be just one value in the parameter.
Here is what I am doing. It is a search form for the user. They can choose value to search by so if they choose Vendor then they can also choose whether they want Open, Closed or Both for the status field. So of course it works just fine if they choose either Open or Closed but if I want it to bring up both Open and Closed I wasn't sure how to do that. Basically I need the parameter to except 1 value or many values. Is that possible? Can you give me an idea where to find directions for doing such a thing.
Thanks so much for your reply.
Stacy (Newbie)
P.S. this is for a fillby query on a winform not for a report. Or is there a way to send something in the parameter that would allow All values in the field, because that is basically what I am doing. They choose open or closed or I want everything. Which way is easier?


nope..if its Multivalued you can even select single values within them

I was under assumption that you're using reporting tool like SSRS. if its an application i think you should be using a long string parameter and passing values in comma separated format like
'Open,Closed,..'

then use string parsing method like below

http://vyaskn.tripod.com/passing_arrays_to_stored_procedures.htm

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

StacyOW
Yak Posting Veteran

93 Posts

Posted - 2012-08-09 : 12:09:46
I read through your link. I haven't used stored procedures yet. So if I am understanding this I would make stored procedures for all of my different searches or fillby queries then and not use the fillby queries?

Because it looks like your stored procedures are dealing with the parameters and then doing the select query.

Thanks for all your help! Eventually I will understand all this only been working with it for 2 months :)
Stacy
P.S. Using sql server 2008 express r2 if that makes any difference.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-09 : 12:42:23
quote:
Originally posted by StacyOW

I read through your link. I haven't used stored procedures yet. So if I am understanding this I would make stored procedures for all of my different searches or fillby queries then and not use the fillby queries?

Because it looks like your stored procedures are dealing with the parameters and then doing the select query.

Thanks for all your help! Eventually I will understand all this only been working with it for 2 months :)
Stacy
P.S. Using sql server 2008 express r2 if that makes any difference.


you need to call the sp with multiple values and send it as comma separated through parameter defined in it. then inside SP you parse individual values out and then use them for filtering results from your actual data tables

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

StacyOW
Yak Posting Veteran

93 Posts

Posted - 2012-08-09 : 14:11:04
That was over my head - guess I'm still to much of a newbie to get that to work, so I just made another query and if the user picks "All" then I run that query instead of the other!

Thanks for all your help.

Stacy
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-09 : 15:14:15
quote:
Originally posted by StacyOW

That was over my head - guess I'm still to much of a newbie to get that to work, so I just made another query and if the user picks "All" then I run that query instead of the other!

Thanks for all your help.

Stacy


ok..cool
if you want try to understand the given approach as I'm quite sure it'll certainly come handy for similar scenarios in future

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2012-08-09 : 15:42:22
read this:
http://www.sommarskog.se/arrays-in-sql.html

the preffered approach is to pass a structured type that maps to a user defined table type.

A multivalued string will work as well but it's not as elegant.


Transact Charlie
Msg 3903.. The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION.
http://nosqlsolution.blogspot.co.uk/
Go to Top of Page

StacyOW
Yak Posting Veteran

93 Posts

Posted - 2012-08-13 : 09:48:25
Visakh16 - I have decided I do need and will need this type of query in the future so I need to get this figured out. I am sending my query as it is now without using the list parameter. So you can see what I am trying to do.
SELECT DISTINCT
porders.PONum, porders.VendorID, porders.OriginatorID, porders.StatusID, porders.ShippedID, porders.Date, porders.MPONum, porders.DateRequired,
porders.RefPONum, porders.ShipTo, porders.ShipAddress, porders.ShipCity, porders.ShipPhone, porders.ShipState, porders.ShipZip, porders.Tax,
porders.Freight, porders.PrintStatus, porders.POTotal, porders.Comments, porders.Description
FROM porders INNER JOIN
items ON porders.PONum = items.PONum INNER JOIN
department ON items.DepartmentID = department.DepartmentID INNER JOIN
postatus ON porders.StatusID = postatus.StatusID
WHERE (department.Department LIKE @SearchValue + '%') AND (postatus.StatusName = @StatusValue)
Now I have tried changing the = to IN and I don't know if it is getting screwed up because I have two parameters. @SearchValue is fine being single parameter, but it's the @StatusValue that I need to be multi-valued. Not sure which Method on your link I should use. I do already have the StatusID in a table of their own but I don't think that helps because it doesn't tell me which values the user wants to get. Also not sure what the To Webuser at the bottom of the sql procedure means. So now I have tried making the sql procedure this is what I have now.
CREATE PROC dbo.GetPOList
(
@StatusList varchar(500)
)
AS
BEGIN
SET NOCOUNT ON

DECLARE @SQL varchar(600)

SET @SQL =
SELECT porders.PONum, porders.VendorID, porders.OriginatorID, porders.StatusID, porders.ShippedID, porders.Date, porders.MPONum, porders.DateRequired,
porders.RefPONum, porders.ShipTo, porders.ShipAddress, porders.ShipCity, porders.ShipPhone, porders.ShipState, porders.ShipZip, porders.Tax, porders.Freight,
porders.PrintStatus, porders.POTotal, porders.Comments, porders.Description
FROM porders INNER JOIN
items ON porders.PONum = items.PONum INNER JOIN
department ON items.DepartmentID = department.DepartmentID
WHERE (department.Department LIKE @SearchValue + '%') AND porders.StatusID IN (' + @StatusList + ')

EXEC(@SQL)
END
GO

GRANT EXEC ON dbo.GetPOList TO WebUser
GO

GRANT SELECT ON dbo.porders TO WebUser
GO

When I run this I get all kinds of errors. Incorrect syntax near the keyword 'SELECT'., Must declare the scalar variable "@SearchValue"., Cannot find the object 'GetPOList', because it does not exist or you do not have permission., Cannot find the user 'WebUser', because it does not exist or you do not have permission. Not sure how to get this to run to test it.
Any help would be much appreciated.
Thanks,
Stacy
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-13 : 09:54:35
you can simply use this


CREATE PROC dbo.GetPOList
(
@SearchValue varchar(100),
@StatusList varchar(500)
)
AS
BEGIN
SET NOCOUNT ON

SELECT porders.PONum, porders.VendorID, porders.OriginatorID, porders.StatusID, porders.ShippedID, porders.Date, porders.MPONum, porders.DateRequired,
porders.RefPONum, porders.ShipTo, porders.ShipAddress, porders.ShipCity, porders.ShipPhone, porders.ShipState, porders.ShipZip, porders.Tax, porders.Freight,
porders.PrintStatus, porders.POTotal, porders.Comments, porders.Description
FROM porders INNER JOIN
items ON porders.PONum = items.PONum INNER JOIN
department ON items.DepartmentID = department.DepartmentID
WHERE (department.Department LIKE @SearchValue + '%')
AND ','+ @StatusList + ',' LIKE '%,' + CAST(porders.StatusID AS varchar)(15)) + ',%'


END
GO


and pass @StatusList values as 'value1,value2,value3,...'

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

StacyOW
Yak Posting Veteran

93 Posts

Posted - 2012-08-13 : 10:38:28
Ok that I understand a little better, but I get an error
Msg 102, Level 15, State 1, Procedure GetPOList, Line 19
Incorrect syntax near '15'.
Is the (15) the varchar size? Cuz it's 500 at top.

Thanks for all your knowledge about this - I really appreciate it.
Stacy
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-13 : 11:36:35
thts a typo



CREATE PROC dbo.GetPOList
(
@SearchValue varchar(100),
@StatusList varchar(500)
)
AS
BEGIN
SET NOCOUNT ON

SELECT porders.PONum, porders.VendorID, porders.OriginatorID, porders.StatusID, porders.ShippedID, porders.Date, porders.MPONum, porders.DateRequired,
porders.RefPONum, porders.ShipTo, porders.ShipAddress, porders.ShipCity, porders.ShipPhone, porders.ShipState, porders.ShipZip, porders.Tax, porders.Freight,
porders.PrintStatus, porders.POTotal, porders.Comments, porders.Description
FROM porders INNER JOIN
items ON porders.PONum = items.PONum INNER JOIN
department ON items.DepartmentID = department.DepartmentID
WHERE (department.Department LIKE @SearchValue + '%')
AND ','+ @StatusList + ',' LIKE '%,' + CAST(porders.StatusID AS varchar(15)) + ',%'


END
GO



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

StacyOW
Yak Posting Veteran

93 Posts

Posted - 2012-08-13 : 21:40:13
Ok I got this working for multiple values which I manually send. The user chooses the "Status" from a combobox. Open, Closed,
Back Ordered, To Order or All. If they choose "All" I manually send 'Open,Closed,Back Ordered,To Order' in the format your query wants. However, when the user just chooses "Open" I say StatusList = POSearchInput.ComboBox1.Text which isn't working. I'm assuming that the query wants 'Open'. So when I assign value from a combobox.text how can I encase the value with the ' ' Or is there a better way to handle this.
You have been so helpful I am so thankful for your help!
Stacy
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-14 : 00:18:59
do a replace to convert " to ' using Replace() function

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

StacyOW
Yak Posting Veteran

93 Posts

Posted - 2012-08-14 : 13:29:43
Visakh16 -
I got it to work :) and I learned a ton of useful functions and ways of doing things.
Thank you so much for taking the time to help others it is greatly appreciated.
Stacy
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-14 : 14:44:53
welcome

glad that I could be of help.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -