SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SSRS Result Set Question


SSRS Result Set Question

Author
Message
rs80
rs80
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1282 Visits: 443
I'm trying to change this sql query from if-else to set-based. However, the first set-based query is picked up by SSRS even if it has 0 records.
Original:

IF @OutputType='WEB'
BEGIN
SELECT ProductID,
ProductName
FROM Product
END
ELSE IF @OutputType = 'CSV'
BEGIN
SELECT ProductID,
ProductName,
ProductDate

FROM Product
END

set-based

SELECT ProductID,
ProductName
FROM Product
WHERE @OutputType='WEB'
SELECT ProductID,
ProductName,
ProductDate
FROM Product
WHERE @OutputType='CSV'

So if the parameter passed is 'CSV' in the set-based query, it returns 0 rows and more than 0 rows in the second query. However, SSRS the dataset would return 0 rows because it'll pick up the result set from the first query.
Is there a way to solve this problem? Or do I have to rely on the if-else logic.


Andrew P
Andrew P
SSCrazy
SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)

Group: General Forum Members
Points: 2546 Visits: 1830
You've got two result-sets there - SSRS will only read the first result-set and ignore the second. UNION ALL them to get the expected result, like below. Note the NULL ProductDate, which makes both result-sets have the same number of columns, required by the UNION operator.

SELECT ProductID,
ProductName,
NULL AS ProductDate
FROM Product
WHERE @OutputType='WEB'
UNION ALL
SELECT ProductID,
ProductName,
ProductDate
FROM Product
WHERE @OutputType='CSV'

rs80
rs80
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1282 Visits: 443
That was easy. Thanks Andrew!
Thom A
Thom A
SSC Guru
SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)

Group: General Forum Members
Points: 53750 Visits: 16682
Although late to the party, why parse the table twice?
SELECT ProductID, ProductName,
CASE @OutputType WHEN 'CSV' THEN ProductDate END AS ProductDate
FROM Product;


(Typed on phone, apologies if any code typing errors).


Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does :-P

Please always remember to encapsulate your code in IFCode Markup. For example [code=sql] [/code].
Click here to read Jeffs Guide on how to post SQL questions, and get swift and helpful answers from the community
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search