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.

这是我的存储过程,我知道问题出在第一部分的某个地方(我可以在其中获取所有运输信息),因为我可以将其注释掉,然后选择摄入的东西,临时表的东西就可以工作了:

UPDATE这里是完整的存储过程

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 回答