1

レポート用にデータを集計しており、一時テーブルに挿入しようとしているストアド プロシージャがあります。実行EXEC <stored_proc_name>するだけで問題なく動作します。ただし、次のコードを実行しようとすると:

Create table #TempResults2
    ( 
       slscode varchar(3),
       intakes_this_month int,
       intakes_this_year int,
       intakes_last_month int,
       intakes_two_months int,
       intakes_three_months int,
       intakes_four_months int,
       intakes_five_months int,
       intakes_six_months int,
       ships_this_month int,
       ships_last_month int,
       ships_two_months int,
       ships_three_months int,
       ships_four_months int,
       ships_five_months int,
       ships_six_months int,
       ships_this_year int
       PRIMARY KEY CLUSTERED (SLSCODE)
       )
       INSERT INTO #TempResults2 
       EXEC crm.dbo.sp_sales_info

       SELECT * FROM #TempResults2

       DROP TABLE #TempResults2

次のエラーが表示されます。

Msg 8114, Level 16, State 1, Procedure sp_sales_info, Line 36
Error converting data type varchar to int.
Warning: Null value is eliminated by an aggregate or other SET operation.

ここに私のストアド プロシージャを示します。問題が最初のセクション (すべての配送情報を取得する場所) のどこかにあることはわかっています。これは、それをコメント アウトして、取り込み対象を選択するだけで一時テーブルが機能するためです。

ここでの更新は完全なストアド プロシージャです

ALTER PROCEDURE [dbo].[sp_sales_info]
    -- Add the parameters for the stored procedure here


AS
BEGIN

    DECLARE 
    @today date, @this_month date, @last_month_start date, @last_month_end date,
    @two_months_start date, @two_months_end date, @three_months_start date, @three_months_end date,
    @four_months_start date, @four_months_end date, @five_months_start date, @five_months_end date,
    @six_months_start date, @six_months_end date, @this_year_start date, @startdate date, @enddate date;

    SET @today = GETDATE();
    SET @this_month = DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0);
    SET @last_month_start = DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + -1, 0);
    SET @last_month_end = DATEADD(MILLISECOND, -3, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + -1 + 1, 0));
    SET @two_months_start = DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + -2, 0);
    SET @two_months_end = DATEADD(MILLISECOND, -3, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + -2 + 1, 0));
    SET @three_months_start = DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + -3, 0);
    SET @three_months_end = DATEADD(MILLISECOND, -3, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + -3 + 1, 0));
    SET @four_months_start = DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + -4, 0);
    SET @four_months_end = DATEADD(MILLISECOND, -3, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + -4 + 1, 0));
    SET @five_months_start = DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + -5, 0);
    SET @five_months_end = DATEADD(MILLISECOND, -3, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + -5 + 1, 0));
    SET @six_months_start = DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + -6, 0);
    SET @six_months_end = DATEADD(MILLISECOND, -3, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + -6 + 1, 0));
    SET @this_year_start = CAST(DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()), 0) as DATE);
    SET @startdate = DATEADD(YEAR, -1, GETDATE());
    SET @enddate = @today;

    SET NOCOUNT ON;

    -- Insert statements for procedure here
SELECT 
    a.*,
    d.intakes_this_month, 
    d.intakes_last_month, 
    d.intakes_two_months, 
    d.intakes_three_months, 
    d.intakes_four_months, 
    d.intakes_five_months, 
    d.intakes_six_months, 
    d.intakes_this_year
FROM
(
SELECT 
            sum(CASE WHEN a.SHIPDATE >= @this_month THEN 1 ELSE 0 END) AS ships_this_month,
            sum(CASE WHEN a.SHIPDATE BETWEEN @last_month_start AND @last_month_end THEN 1 ELSE 0 END) AS ships_last_month,
            sum(CASE WHEN a.SHIPDATE BETWEEN @two_months_start AND @two_months_end THEN 1 ELSE 0 END) AS ships_two_months,
            sum(CASE WHEN a.SHIPDATE BETWEEN @three_months_start AND @three_months_end THEN 1 ELSE 0 END) AS ships_three_months,
            sum(CASE WHEN a.SHIPDATE BETWEEN @four_months_start AND @four_months_end THEN 1 ELSE 0 END) AS ships_four_months,
            sum(CASE WHEN a.SHIPDATE BETWEEN @five_months_start AND @five_months_end THEN 1 ELSE 0 END) AS ships_five_months,
            sum(CASE WHEN a.SHIPDATE BETWEEN @six_months_start AND @six_months_end THEN 1 ELSE 0 END) AS ships_six_months,
            sum(CASE WHEN a.SHIPDATE >= @this_year_start THEN 1 ELSE 0 END) AS ships_this_year,
            b.slcode
FROM 
(
    SELECT
        A.ACCOUNT AS CODE,
        MIN(CAST(A.BILLDATETIME AS DATE)) AS SHIPDATE

    FROM PACWARE.ADS.ARODME A
    LEFT OUTER JOIN PACWARE.ADS.PTDME B ON A.PTCODE=B.CODE_
    LEFT OUTER JOIN event.dbo.newdate() D ON A.ACCOUNT=D.ACCOUNT
    LEFT OUTER JOIN event.dbo.newdate_extras() D2 ON A.ACCOUNT=D2.ACCOUNT
    WHERE A.BILLDATETIME>=@startdate
    AND A.BILLDATETIME<=@enddate
    AND (
        (D.NEWDATE>=@startdate AND D.NEWDATE<=@enddate) OR
        (D2.NEWDATE IS NOT NULL AND D2.NEWDATE>=@startdate AND D2.NEWDATE<=@enddate) OR
        B.MEDICAREID='L7900' 
        )
    AND B.MEDICAREID IN ('A4253','L7900','A9276')
    AND A.CATEGORY<>'ID'
    Group by 
    A.ACCOUNT,
    B.MEDICAREID,
    A.CATEGORY
) a
JOIN event.dbo.patient_dg() b on a.CODE = b.code
GROUP BY b.slcode
) a 
JOIN (
    SELECT 
            sum(CASE WHEN a.regdate >= @this_month THEN 1 ELSE 0 END) AS intakes_this_month,
            sum(CASE WHEN a.regdate BETWEEN @last_month_start AND @last_month_end THEN 1 ELSE 0 END) AS intakes_last_month,
            sum(CASE WHEN a.regdate BETWEEN @two_months_start AND @two_months_end THEN 1 ELSE 0 END) AS intakes_two_months,
            sum(CASE WHEN a.regdate BETWEEN @three_months_start AND @three_months_end THEN 1 ELSE 0 END) AS intakes_three_months,
            sum(CASE WHEN a.regdate BETWEEN @four_months_start AND @four_months_end THEN 1 ELSE 0 END) AS intakes_four_months,
            sum(CASE WHEN a.regdate BETWEEN @five_months_start AND @five_months_end THEN 1 ELSE 0 END) AS intakes_five_months,
            sum(CASE WHEN a.regdate BETWEEN @six_months_start AND @six_months_end THEN 1 ELSE 0 END) AS intakes_six_months,
            sum(CASE WHEN a.regdate >= @this_year_start THEN 1 ELSE 0 END) AS intakes_this_year,
            a.slscode
    FROM 
    event.dbo.patient_dg_lite() a
    GROUP BY a.slscode
) d on a.slcode = d.slscode
JOIN event.dbo.employee_slscode b on a.slcode = b.slscode
JOIN event.dbo.employee c on b.employee_id = c.id
END
4

1 に答える 1

1

あなたの列は同じ序数の位置にありません でslscode最初に定義されていCREATE TABLEます。

Create table #TempResults2
    ( 
       slscode varchar(3),
      /*Rest of table*/
      ships_this_year int
     )

しかしSELECT、ストアドプロシージャには最後の列としてそれがあります

SELECT 
       /* Loads of CASE statements*/
        b.slcode
FROM /* ... */

したがって、slscode varchar列を整数ships_this_year列に挿入しようとしています

于 2013-01-05T19:02:10.607 に答える