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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 [RESOLVED] multiple values for one parameter

Author  Topic 

HenryFulmer
Posting Yak Master

110 Posts

Posted - 2011-04-28 : 11:03:15
I need to write a query that returns a result set based on a parameter that is passed to it.
My table contains records of customer support calls. For the review of these calls I need to be able to pass in values for the topics of these calls. This can be one or more based on user selection.
Here's my table:

CREATE TABLE Tbl1 (
Customer varchar(50),
Country char(3),
Topic varchar(50),
CallCount int,
CallMonth char(10)
)
--Insert records in source table Tbl1
INSERT INTO Tbl1 VALUES('10001','MX','Shipping Advise',1,'2011-01')
INSERT INTO Tbl1 VALUES('10002','CA','Tariff Request',2,'2011-01')
INSERT INTO Tbl1 VALUES('10003','US','Consolidation',1,'2011-01')
INSERT INTO Tbl1 VALUES('10003','US','Shipping Advise',3,'2011-01')
INSERT INTO Tbl1 VALUES('10001','MX','Tariff Request',2,'2011-03')
INSERT INTO Tbl1 VALUES('10002','CA','Consolidation',3,'2011-03')
INSERT INTO Tbl1 VALUES('10004','US','Shipping Advise',2,'2011-02')
INSERT INTO Tbl1 VALUES('10003','US','Tariff Request',5,'2011-02')
INSERT INTO Tbl1 VALUES('10001','MX','Consolidation',2,'2011-02')
INSERT INTO Tbl1 VALUES('10002','CA','Consolidation',1,'2011-02')

DECLARE @Topic VARCHAR(2000)

SELECT Customer, Country, Topic, SUM(CallCount) as CallCount
FROM Tbl1
WHERE Topic = @Topic
GROUP BY Customer, Country, Topic



1. If I wanted to pass in the value(s): 'Shipping Advise' and 'Consolidation' for the @topic variable. How would I do that.
2. How would I return all records when nothing is passed in. Can I set a default?

Thanks for your help.

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-04-28 : 11:08:58
1. read these links
http://www.sommarskog.se/arrays-in-sql.html
http://vyaskn.tripod.com/passing_arrays_to_stored_procedures.htm

2. pass in NULL value into @Topic. Check for NULL value in where clause

WHERE @Topic is NULL
or Topic = @Topic



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

HenryFulmer
Posting Yak Master

110 Posts

Posted - 2011-04-28 : 11:41:56
Thank you.
Go to Top of Page
   

- Advertisement -