Sql server 2008 存储过程引发错误,但作为查询运行不会引发错误
我正在聚合用于报告的数据,并且有一个存储过程,我正试图将其插入到一个临时表中。如果我只是运行Sql server 2008 存储过程引发错误,但作为查询运行不会引发错误,sql-server-2008,stored-procedures,temp-tables,Sql Server 2008,Stored Procedures,Temp Tables,我正在聚合用于报告的数据,并且有一个存储过程,我正试图将其插入到一个临时表中。如果我只是运行EXEC,它工作得很好。但是,当我尝试运行以下代码时: Create table #TempResults2 ( slscode varchar(3), intakes_this_month int, intakes_this_year int, intakes_last_month int, intakes_two_mont
EXEC
,它工作得很好。但是,当我尝试运行以下代码时:
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
ALTER PROCEDURE[dbo].[sp\u sales\u info]
--在此处添加存储过程的参数
作为
开始
声明
@今天日期、@本月日期、@上月开始日期、@上月结束日期、,
@两个月开始日期、@两个月结束日期、@三个月开始日期、@三个月结束日期、,
@四个月开始日期、@四个月结束日期、@五个月开始日期、@五个月结束日期、,
@六个月开始日期、@六个月结束日期、@本年开始日期、@startdate日期、@enddate日期;
设置@today=GETDATE();
设置@this_month=DATEADD(month,DATEDIFF(month,0,GETDATE()),0);
设置@last\u month\u start=DATEADD(month,DATEDIFF(month,0,GETDATE())+-1,0);
设置@last_month_end=DATEADD(毫秒,-3,DATEADD(month,DATEDIFF(month,0,GETDATE())+-1+1,0));
设置@two\u months\u start=DATEADD(MONTH,DATEDIFF(MONTH,0,GETDATE())+-2,0);
设置@two_months_end=DATEADD(毫秒,-3,DATEADD(MONTH,DATEDIFF(MONTH,0,GETDATE())+-2+1,0));
设置@three\u months\u start=DATEADD(MONTH,DATEDIFF(MONTH,0,GETDATE())+-3,0);
设置@three_months_end=DATEADD(毫秒,-3,DATEADD(MONTH,DATEDIFF(MONTH,0,GETDATE())+-3+1,0));
设置@four\u months\u start=DATEADD(MONTH,DATEDIFF(MONTH,0,GETDATE())+-4,0);
设置@four_months_end=DATEADD(毫秒,-3,DATEADD(MONTH,DATEDIFF(MONTH,0,GETDATE())+-4+1,0));
设置@five\u months\u start=DATEADD(MONTH,DATEDIFF(MONTH,0,GETDATE())+-5,0);
设置@five_months_end=DATEADD(毫秒,-3,DATEADD(MONTH,DATEDIFF(MONTH,0,GETDATE())+-5+1,0));
设置@six\u months\u start=DATEADD(MONTH,DATEDIFF(MONTH,0,GETDATE())+-6,0);
设置@six_months_end=DATEADD(毫秒,-3,DATEADD(MONTH,DATEDIFF(MONTH,0,GETDATE())+-6+1,0));
将@this\u year\u start=CAST(DATEADD(year,DATEDIFF(year,0,GETDATE()),0)设置为日期);
SET@startdate=DATEADD(YEAR,-1,GETDATE());
设置@enddate=@今天;
不计数;
--在此处插入过程的语句
挑选
a、 *,
d、 本月入学人数,
d、 上个月的入学人数,
d、 入学两个月,
d、 入学三个月,
d、 入学四个月,
d、 入学五个月,
d、 入学六个月,
d、 今年的入学人数
从…起
(
挑选
总和(如果a.SHIPDATE>=@本月,则为1,否则为0结束)作为本月发货,
总和(当a.SHIPDATE介于@last_month_start和@last_month_end之间,然后为1或0 end)作为ships_last_month,
总和(当a.SHIPDATE介于@two_months_start和@two_months_end之间时,则为1,否则为0 end)作为ships_two_months,
总和(当a.SHIPDATE介于@three_months_start和@three_months_end之间时,则为1,否则为0 end)作为ships_three_months,
总和(如果一个发货日期介于@4个月\u开始和@4个月\u结束之间,则为1,否则为0结束)为发货4个月,
总和(如果一个发货日期介于@5个月\u开始和@5个月\u结束之间,则为1,否则为0结束)为发货五个月,
总和(如果一个装运日期介于@six_months_start和@six_months_end之间,则为1,否则为0 end)作为装运六个月,
总和(当a.SHIPDATE>=@本年开始,然后1,否则0结束)作为本年发货,
b、 slcode
从…起
(
挑选
A.帐户作为代码,
最小值(铸造日期(A.BILLDATETIME为日期))为装运日期
来自PACWARE.ADS.A
左侧外部连接PACWARE.ADS.PTDME B上的A.PTCODE=B.CODE_
A.ACCOUNT=D.ACCOUNT上的LEFT OUTER JOIN event.dbo.newdate()D
A.ACCOUNT=D2.ACCOUNT上的左外部联接事件.dbo.newdate_extras()D2
其中A.BILLDATETIME>=@startdate
和A.BILLDATETIME=@startdate和D.NEWDATE=@startdate和D2.NEWDATE=@this\u month然后1或0 END)作为本月的摄入量,
作为上月入学人数的总和(如果a.regdate介于@last_month_start和@last_month_end之间,则为1,否则为0 end),
总(当a.regdate介于@two\u months\u start和@two\u months\u end之间时,则为1,否则为0 end)作为两个月的取数,
作为三个月入学人数的总和(如果a.regdate介于三个月开始和三个月结束之间,则为1,否则为0结束),
总计(如果a.regdate介于@four_months_开始和@four_months_结束之间,则为1,否则为0结束)作为四个月的取数,
总和(当a.regdate介于@five_months_start和@five_months_end之间时,则为1,否则为0 end)作为五个月的摄入量,
总(当a.regdate介于@six_months_start和@six_months_end之间时,则为1,否则为0 end)作为六个月入学人数,
总和(当a.regdate>=@本年开始,然后1,否则0结束)作为本年的入学人数,
a、 slscode
从…起
event.dbo.patient_dg_lite()a
按a.slscode分组
)a.slcode=d.slscode上的d
在a.slcode=b.slscode上加入event.dbo.employee\u slscode b
在b.employee_id=c.id上加入event.dbo.employee c
结束
您的列不在相同的顺序位置slscode
首先在创建表中定义
Create table #TempResults2
(
slscode varchar(3),
/*Rest of table*/
ships_this_year int
)
但是存储过程中的选择将其作为最后一列
SELECT
/* Loads of CASE statements*/
b.slcode
FROM /* ... */
因此,您试图将slscode
varchar
列插入整型ships\u this\u year
列中您的列不在相同的顺序位置slscode
首先在创建表中定义
Create table #TempResults2
(
slscode varchar(3),
/*Rest of table*/
ships_this_year int
)
但是存储过程中的选择将其作为最后一列
SELECT
/* Loads of CASE statements*/
b.slcode
FROM /* ... */
你也是