【问题标题】:Code works in Management Studio but not in the Dataset of an .rdl report代码在 Management Studio 中有效,但在 .rdl 报告的数据集中无效
【发布时间】:2015-09-16 14:55:06
【问题描述】:

为什么这只适用于管理工作室而不适用于 rdl 报告的数据集?

我无权访问实时数据库,因此无法将此功能直接添加到数据库中。

if OBJECT_ID (N'dbo.PreviousFriday', N'FN') is not null
    drop function dbo.PreviousFriday;
go
create function dbo.PreviousFriday (@Date datetime)
returns datetime
with execute as caller
as
begin
    declare @PreviousFriday datetime;

    set @PreviousFriday = DATEADD(DAY, (case DATEPART(DW, @Date)
        when 1 then -2
        when 2 then -3
        when 3 then -4
        when 4 then -5
        when 5 then -6
        when 6 then 0
        when 7 then -1
    end), @Date)
    return(@PreviousFriday);
end
go

if OBJECT_ID (N'dbo.NextFriday', N'FN') is not null
    drop function dbo.NextFriday;
go
create function dbo.NextFriday (@Date datetime)
returns datetime
with execute as caller
as
begin
    declare @NextFriday datetime;

    set @NextFriday = DATEADD(DAY, (case DATEPART(DW, @Date)
        when 1 then 5
        when 2 then 4
        when 3 then 3
        when 4 then 2
        when 5 then 1
        when 6 then 0
        when 7 then 6
    end), @Date)
    return(@NextFriday);
end
go

这是我得到的错误:

【问题讨论】:

    标签: sql-server-2008 tsql reporting-services rdl


    【解决方案1】:

    因为SSRS中的查询需要是DML(Data Manipulation Language)

     select * from ...
    

     exec dbo.prcGetMyData ...
    

    那种东西……


    但是您正在尝试输入 DDL(数据定义语言),即您正在尝试更改底层数据库,如果允许的话,这将是一个安全问题。


    您可以创建一个名为 LastFriday 的数据集,其中包含您的脚本。然后,您可以在文本框中或任何地方使用数据集的值...

    SELECT    DATEADD(DAY, (CASE DATEPART(DW, @Date) 
                             WHEN 1 THEN - 2 
                             WHEN 2 THEN - 3 
                             WHEN 3 THEN - 4 
                             WHEN 4 THEN - 5 
                             WHEN 5 THEN - 6 
                             WHEN 6 THEN 0 
                             WHEN 7 THEN - 1 END), @Date) AS LastFriday
    

    如果您想在多个报告之间共享它,请创建一个共享数据集。 (您需要为 NextFriday 做同样的事情!)

    或者您可以完全避免使用 TSQL,只需在报告中创建函数作为代码。

    【讨论】:

    • 谢谢,看来我需要绕过 tsql
    猜你喜欢
    • 1970-01-01
    • 2023-03-29
    • 2020-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多