简体   繁体   English

SSRS错误添加共享数据集

[英]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: 当我将以下代码添加到报表存储过程中时,无法将共享数据集添加到我的SSRS报表中,这给了我这个错误:

在此处输入图片说明

Incorrect syntax near the keyword 'as'. 关键字“ 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. sql语句将数据附加到临时表中,该表中包含上一条语句中已有的数据。
@TimeDefinition is declared as NVARCHAR(100) @TimeDefinition声明为NVARCHAR(100)
Replacing @TimeDefinition with the text that is in the variable (example cast(CREATEDDATE as date ) allows the report to be added 将@TimeDefinition替换为变量中的文本(例如cast(CREATEDDATE as date ),可以添加报告
if I remove the AS from the concatenated string, then the error simply changes to Incorrect syntax near the keyword 'by' . 如果我从串联字符串中删除了AS ,则该错误仅更改为关键字'by'附近的Incorrect语法
Running the query in SSMS does not give an error 在SSMS中运行查询不会给出错误

The problem here is that SSRS wasn't able to determine the columns of the query without a parameter being supplied. 这里的问题是,在没有提供参数的情况下,SSRS无法确定查询的列。 When using dynamic SQL, sometimes the dynamic SQL won't have a value if no value is supplied for one (or more) parameters; 使用动态SQL时,如果一个(或多个)参数没有提供值,则有时动态SQL将没有值。 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 . 除非提供@Col1@Col2的值, @SQL的值将导致NULL

When using queries such as these, SSRS will therefore present you with a dialogue window. 因此,在使用此类查询时,SSRS将为您提供一个对话窗口。 This'll look something like the below (taken from SSDT 2017) 如下所示(取自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. 您需要在此对话框中输入一些值(在第二列中,名为Parameter Value ),以便SSRS可以正确确定列定义。 Once you've done that, SSRS will create the dataset correctly. 完成此操作后,SSRS将正确创建数据集。

On a related note, I suggest against the use of 1=1 . 在相关说明中,我建议不要使用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. 这只会增加处理时间(因为数据引擎仍需要检查1=1 ),并且可能导致查询分析器做出错误的选择。 It actually can be considered quite bad practice to put things like that in your query's WHERE clause. 实际上,将类似的内容放入查询的WHERE子句中可能被认为是非常糟糕的做法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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