【问题标题】:Stored Procedure Throws Error, but running as a query does not存储过程引发错误,但作为查询运行不会
【发布时间】:2012-12-19 22:25:24
【问题描述】:

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

【问题讨论】:

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


    【解决方案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

    【讨论】:

    • 我会发布完整的存储过程供您查看(我在完整声明中将 slcode 别名为 slscode)
    • @Mike - 你给它取什么别名并不重要。位置才是最重要的(除非您的意思是使用INSERT 的列列表)
    • 啊,我想我明白你在说什么了。我会进行编辑,如果这能解决问题,我会通知您。
    • 其实不是最后一栏。一直在看错位。将是从 a.* 返回的最后一列的第 9 列,因此将进入 intakes_six_months,这仍然是错误的列。
    • 感谢马丁的帮助,你的救命稻草。这只是列的顺序。我没有使用 *,而是从结果中定义了每一列,并确保我的 select into 语句匹配。现在它按预期工作。 +1 并肯定回答。
    猜你喜欢
    • 2013-10-08
    • 2012-03-13
    • 1970-01-01
    • 2018-09-13
    • 1970-01-01
    • 2014-01-22
    • 1970-01-01
    • 2016-03-28
    • 1970-01-01
    相关资源
    最近更新 更多