openquery传递日期参数转换失败

时间:2017-04-12 13:46:15

标签: sql-server dataset ssrs-2012 data-conversion openquery

创建以下存储过程以在SSRS中创建数据集。但是当我在SSRS中运行查询时,我从字符串转换日期时得到转换失败错误。我确定它是语法或变量问题。有什么想法吗?

   ALTER PROCEDURE [dbo].[usp_r_pcdh_counts]
     @START datetime,
     @END datetime
       AS
       BEGIN
       SET NOCOUNT ON;
   DECLARE @START1 datetime= @START 
   DECLARE @END1 datetime = @END
   DECLARE @sql nvarchar (4000)
   SET @sql= 'SELECT *
   FROM OPENQUERY(MR_UNI,''select 
                count(case when event_audit_type_key in ("1346","1038") then 1 else null end) as outsuccessful,
                count(case when event_audit_type_key = "1531" then 1 else null end) as outunsuccessful,
                count( case when event_audit_type_key in ("1040") then 1 else null end) as outdocsfound,
                count( case when event_audit_type_key in ("1532") then 1 else null end) as outdocsnotfound,
                count( case when event_audit_type_key in ("1042") then 1 else null end) as outdocsretrieved,
                count( case when event_audit_type_key in ("1580") then 1 else null end) as outdocsnotretrieved,
                count( case when event_audit_type_key in ("1048") then 1 else null end) as insuccess,
                count( case when event_audit_type_key in ("1048") and success_code = "12"  then 1 else null end) as infailure,
                count( case when event_audit_type_key in ("1050") then 1 else null end) as indocsretrieved
            from public.event_audit ea
            where ea.event_date >= convert(date,''''' + @START1+ ''''', 110)
                    and ea.event_date < convert(date, '''''+ @END1+ ''''', 110)
                    AND event_audit_key >= (SELECT MAX(event_audit_key) - 100 
                         FROM reports.event_audit_index 
                         WHERE event_date <= convert(date, '''''+ @START1+ ''''', 110))
                    AND event_audit_key <= (SELECT MIN(event_audit_key) + 100 
                         FROM reports.event_audit_index 
                          WHERE event_date >= convert(date, '''''+ @END1+''''', 110))'')'
    EXEC @sql
    END

1 个答案:

答案 0 :(得分:0)

你是双嵌套动态SQL(DSQL),这只是一个痛苦的屁股。诀窍是使用简单的print语句弹出一层动态逻辑,以检查该级别的语法,这将有助于您更好地解决这类问题。

无论如何,这应该给你正在寻找:

ALTER PROCEDURE [dbo].[usp_r_pcdh_counts]
    @START datetime,
    @END datetime
    AS
    BEGIN
    SET NOCOUNT ON;
DECLARE @START1 datetime= @START 
DECLARE @END1 datetime = @END
DECLARE @sql nvarchar (4000)
SET @sql= 'SELECT *
FROM OPENQUERY(MR_UNI,''select 
            count(case when event_audit_type_key in (1346,1038) then 1 else null end) as outsuccessful,
            count(case when event_audit_type_key = 1531 then 1 else null end) as outunsuccessful,
            count( case when event_audit_type_key in (1040) then 1 else null end) as outdocsfound,
            count( case when event_audit_type_key in (1532) then 1 else null end) as outdocsnotfound,
            count( case when event_audit_type_key in (1042) then 1 else null end) as outdocsretrieved,
            count( case when event_audit_type_key in (1580) then 1 else null end) as outdocsnotretrieved,
            count( case when event_audit_type_key in (1048) then 1 else null end) as insuccess,
            count( case when event_audit_type_key in (1048) and success_code = 12  then 1 else null end) as infailure,
            count( case when event_audit_type_key in (1050) then 1 else null end) as indocsretrieved
        from public.event_audit ea
        where ea.event_date >= convert(date, ''''' + convert(varchar, @START1, 110)+ ''''', 110)
                and ea.event_date < convert(date, '''''+ convert(varchar, @END1, 110) + ''''', 110)
                AND event_audit_key >= (SELECT MAX(event_audit_key) - 100 
                        FROM reports.event_audit_index 
                        WHERE event_date <= convert(date, '''''+ convert(varchar, @START1, 110) + ''''', 110))
                AND event_audit_key <= (SELECT MIN(event_audit_key) + 100 
                        FROM reports.event_audit_index 
                        WHERE event_date >= convert(date, '''''+ convert(varchar, @END1, 110) +''''', 110))'')'
--PRINT @sql
EXEC @sql
END