简体   繁体   中英

SSRS error adding shared dataset

When I add the following code into my report stored procedure, I cannot add the shared dataset to my SSRS report, and it gives me this error:

在此处输入图片说明

Incorrect syntax near the keyword 'as'.

The code I have added (to a previously working procedure) is:

set @sqlstring = Concat(N'INSERT INTO #TEXT_SEARCH_RESULT',
        N'(Result_Name,Count_of_tickets,HT,Created_Group)',
        N' SELECT ',@TimeDefinition,' as RESULT_NAME',
        N' ,sum(COUNT_OF_TICKETS) as COUNT_OF_TICKETS',
        N' ,sum(HT) as HT',
        N' ,''Group'' as CREATED_GROUP',
        N' from #TEXT_SEARCH_MAIN ct',
        N' where (1=1) ')
--other items that append to the string, but have no bearing on the question
set @sqlstring=concat(@sqlstring,N' group by ',@TimeDefinition)

The sql statement appends data to an temporary table with data already in it from the previous statement.
@TimeDefinition is declared as NVARCHAR(100)
Replacing @TimeDefinition with the text that is in the variable (example cast(CREATEDDATE as date ) allows the report to be added
if I remove the AS from the concatenated string, then the error simply changes to Incorrect syntax near the keyword 'by' .
Running the query in SSMS does not give an error

The problem here is that SSRS wasn't able to determine the columns of the query without a parameter being supplied. When using dynamic SQL, sometimes the dynamic SQL won't have a value if no value is supplied for one (or more) parameters; thus the column values can't be determined. Take a simple example like:

DECLARE @SQL nvarchar(MAX);

SET @SQL = N'SELECT ' + QUOTENAME(@Col1) + N' AS Result1,' + NCHAR(10) + 
           N'       ' + QUOTENAME(@Col2) + N' AS Result2' + NCHAR(10) + 
           N'FROM MyTable;';
PRINT @SQL;
EXEC sp_executesql @SQL;

Unless values for @Col1 and @Col2 are supplied, then the value of @SQL will result in NULL .

When using queries such as these, SSRS will therefore present you with a dialogue window. This'll look something like the below (taken from SSDT 2017) 在此处输入图片说明

You'll need to enter some values into this dialogue (in the second column, which is named Parameter Value ) so that SSRS can correctly determine the column definitions. Once you've done that, SSRS will create the dataset correctly.

On a related note, I suggest against the use of 1=1 . This just adds time to the processing (as the data engine still needs to check if 1=1 ) and can cause the query analyser to make poor choices. It actually can be considered quite bad practice to put things like that in your query's WHERE clause.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM