Reporting services,输入参数组合sql的问题

yuemb369 2007-08-01 04:58:59
其中CustomerNo是要在报表中显示的字段,@StartDate是报表参数,我的基本想法是:如果@StartDate为空,就查全部,如果不为空,就加入查询条件. 运行报表有问题,但是我在设计报表时手动测试查询(就是点那个!号),输入参数值,是可以运行的。

SQL:

declare @sql nvarchar(4000)
declare @sqlWhere nvarchar(4000)

set @sql='select CustomerNo from table1 '
set @sqlWhere=''


if @StartDate is not null and @StartDate<>''
begin
set @sqlWhere =@sqlWhere + ' StartDate =''' +@StartDate + ''''
end

if ltrim(rtrim(@sqlWhere))<>''
begin
set @sqlWhere =' where ' + @sqlWhere
end
set @sql=@sql+@sqlWhere

exec sp_executeSQL @sql


------------------------
Error message:
[rsFieldReference] The Value expression for the textbox ‘CustomerNo’ refers to the field ‘CustomerNo’. Report item expressions can only refer to fields within the current data set scope or, if inside an aggregate, the specified data set scope.

如果我不加入这个条件,只是
set @sql='select CustomerNo from table1 '
也不会出问题
...全文
191 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
yaoleshi 2008-04-23
  • 打赏
  • 举报
回复
我还可以接几分
yuemb369 2007-08-15
  • 打赏
  • 举报
回复
kid_1982() ( ) 信誉:100 2007-8-2 9:56:42 得分: 0



set @sqlWhere =@sqlWhere + ' StartDate =''' +@StartDate + ''''
改为
set @sqlWhere =@sqlWhere + ' StartDate =' + ''' +@StartDate + '''
呢?



不是这个原因,是因为我没有在Parameters里指定StartDate等的对应值。谢谢你的回复
kid_1982 2007-08-02
  • 打赏
  • 举报
回复
set @sqlWhere =@sqlWhere + ' StartDate =''' +@StartDate + ''''
改为
set @sqlWhere =@sqlWhere + ' StartDate =' + ''' +@StartDate + '''
呢?
yuemb369 2007-08-02
  • 打赏
  • 举报
回复
up
yuemb369 2007-08-01
  • 打赏
  • 举报
回复
没有分了,我只有十分

4,816

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 图表区
社区管理员
  • 图表区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧