views:

1626

answers:

3

In the SQL queries for my reporting services datasets, I'd like to be able to use the following pattern:

-- Used to enable/disable diagnostic statements
declare @DEBUG bit
set @DEBUG = 0

-- Get current date/time.
declare @now datetime; set @now = getdate()
if @DEBUG = 1 select @now as now

-- Create a table to contain the chart values for the last 30 days.
declare @reportValues table
(
    reportDate datetime,
    x int
)

-- Populate the table with the last 30 days.
declare @counter int; set @counter = 0
while @counter < 5
begin
    insert into @reportValues select dateadd( day, -@counter, @now ), @counter
    set @counter = @counter + 1
end
if @DEBUG = 1 select * from @reportValues

select sum(x) as total from @reportValues

The problem is, Reporting Services ignores the IF statements and "select @now as now" for the report's values, instead of the main query.

Is there a way to accomplish this pattern that gets around this RS limitation?

Update: If you go to the "SET FMTONLY" help page on the MSDN, someone has already noted this problem in a comment at the bottom of the page, and that turning off this option fixes this problem.

A: 

You would be better off writing your query in a stored procedure and calling the sproc from RS instead of having to rely on RS's interpretation of your text query. Then you should be able to pass in your @Debug parameter and check on it without issue.

Edit: Try this out instead and see if it recognizes the If statement -

-- Used to enable/disable diagnostic statements
declare @DEBUG bit
set @DEBUG = 0

-- Get current date/time.
declare @now datetime; set @now = getdate()
IF @DEBUG = 1 
BEGIN
    select @now as now
END
ELSE
BEGIN
    select * from ... -- Main query elided
END

Edit #2: Since the above did not seem to help, my next suggestion would be to attempt something like a SELECT CASE (This is assuming that you are only pulling one column and calling it "now"):

-- Used to enable/disable diagnostic statements
declare @DEBUG bit
set @DEBUG = 0

-- Get current date/time.
declare @now datetime; set @now = getdate()
SELECT
    CASE @Debug
        WHEN 1 THEN @now
        WHEN 0 THEN (Perform Your Select Inside These Parentheses)
    END as now
TheTXI
Hmmm, it's a custom report for one of our customers. I'm hesitant to create anything external to the RDL file at this point.
Marcel Lamothe
Good idea, but still not working - same problem occurs.
Marcel Lamothe
That's strange. I've done things like this before and never really had any problems.
TheTXI
In all cases, the SQL executes correctly. It's only when I run the report that it goes dumb.
Marcel Lamothe
Grr. It doesn't make a whole lot of sense for the SQL to work perfectly as intended and then for Reporting Services to hose it up like this. Is it possible that you could give some more information in your original topic concerning what you are actually trying to do (away from the SQL itself)? We may be able to find out if there is something else funky going on elsewhere in your report.
TheTXI
It's not specific to this query, I've tried this in the past and couldn't get it to work. Just figured I'd ask SO finally. As for RS, I've found a lot of quirks and annoyances over the years, so it this limitation doesn't surprise me to much. I'm using RS 2005 for what it's worth.
Marcel Lamothe
+1  A: 

Does the column set vary with @debug?

If the database is SQL Server, you might try the fmtonly kludge:

declare @fmtonlyon bit
select @fmtonlyon = 0
if 1 = 0 select @fmtonlyon = 1
if @fmtonlyon = 1 set fmtonly off

/* query body */

if @fmtonlyon = 1 set fmtonly on

It forces full evaluation. Might or might not work in your situation.

Alternatively, replace the debug SELECT statements with PRINT.

Peter
Answers: The column set does not vary with @debug. The database IS SQL server. The kludge didn't work unfortunately.
Marcel Lamothe
I like the PRINT idea, and it does solve this issue of showing the value of @now. Is there a way I could do the same for a temporary table?
Marcel Lamothe
It'd be easier if you posted the full query, or something that reproduces the problem with minimal dependencies so we see your intent and reproduce the behavior ourselves.
Peter
Example Updated.
Marcel Lamothe
Thanks. Now, in SSRS dataset editor, put SET FMTONLY OFF in the first line of the query, click the refresh button, and the dataset metadata should update from "now" to "total". But, the report will still break if you set @debug = 1, since the column names will have changed and any references to "total" will raise an error.
Peter
A: 

I am still stuck...When I use SET FMTONLY OFF, it gives me an error the construct SET is not supported....any ideas?

Abhi
Can you post a bit of code?
Marcel Lamothe