存储过程引发错误,但作为查询运行不会

时间:2013-01-05 18:14:43

标签: sql-server-2008 stored-procedures temp-tables

我正在汇总报告数据,并且有一个我试图插入临时表的存储过程。如果我只是运行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

1 个答案:

答案 0 :(得分: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