2017-04-12 19 views
0

SSRS内でデータセットを作成するために、次のストアドプロシージャを作成しました。しかし、SSRSでクエリを実行すると、文字列から日付を変換するときに変換エラーが発生します。私はそれが構文か可変の問題であると確信しています。何か案は?openquery passing dateパラメータの変換に失敗しました。

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 

答えて

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 
+0

おかげで、私は仕事に変換(varchar型)の外側に別の引用符を追加しましたが、私は今、エラー「名「を選択*からを取得していますopenquery ....は有効な識別子ではありません "と表示され、openquery select文が表示されます。私はEXEC(@ sql)を追加しましたが、これは助けに思えましたが、今は "構文が正しくありません" – Wendi

+0

@Wendi - これらのキーフィールドがINTSとして定義されていることを願っているので、 VARCHARではなくそれはあなたの問題かもしれません。また、ネスティングが私の答えに適切であるべきであるので、私はあなたがもう引用符を追加する必要はないと思う。 –

関連する問題