您好,本论坛的所有成员, 我正在尝试使用以下存储过程将数据从主表插入临时表以进一步分析:
ALTER PROCEDURE [dbo].[terr_punctuality_comp] AS
Begin
CREATE TABLE #previousdata ( Rly nvarchar(255), preDirect numeric(18,0), preIndirect numeric(18,0), preIncidences numeric(18,0), preTotal numeric(18,0))
INSERT INTO #previousdata SELECT SUM(case when Dir_Ind = 'Dir' then 1 else 0 end ) AS 'preDirect', SUM(case when Dir_Ind = 'Ind' then 1 else 0 end ) +
SUM(case when rep1 IS NOT NULL then 1 else 0 end)
+ SUM(case when rep2 IS NOT NULL then 1 else 0 end)
+ SUM(case when rep3 IS NOT NULL then 1 else 0 end)
+ SUM(case when rep4 IS NOT NULL then 1 else 0 end)
+ SUM(case when rep5 IS NOT NULL then 1 else 0 end)AS 'preIndirect',
SUM(case when Dir_Ind IS NOT NULL then 1 else 0 end ) AS 'preIncidences',
SUM(case when Dir_Ind = 'Dir' then 1 else 0 end ) + SUM(case when Dir_Ind = 'Ind' then 1 else 0 end ) +
SUM(case when rep1 IS NOT NULL then 1 else 0 end)
+ SUM(case when rep2 IS NOT NULL then 1 else 0 end)
+ SUM(case when rep3 IS NOT NULL then 1 else 0 end)
+ SUM(case when rep4 IS NOT NULL then 1 else 0 end)
+ SUM(case when rep5 IS NOT NULL then 1 else 0 end)AS 'preTotal',
Rly FROM PunctualityMain
WHERE Rly IN ('CR', 'ER', 'ECR', 'ECoR', 'NR', 'NCR', 'NER', 'NFR', 'NWR', 'SR', 'SCR', 'SER', 'SECR', 'SWR', 'WR', 'WCR')
GROUP BY Rly
end
执行时我正在
将数据类型nvarchar转换为数字时出错。
我的主表结构是
ID int
Date datetime
Train int
Dir_Ind nvarchar(255)
Detn int
Rly nvarchar(255)
DiV nvarchar(255)
rep1 int
det1 int
rep2 int
det2 int
rep3 int
det3 int
rep4 int
det4 int
rep5 int
det5 int
先生,如何解决这个问题?
答案 0 :(得分:1)
我猜你不能依靠别名来设置哪个选定字段转到哪个表字段。相反,您应该在插入中指定字段顺序...选择如:
INSERT INTO #previousdata(PreDirect,preIndirect,preIncidences,preTotal,Rly)SELECT ...;
否则,数据库会尝试将第五个选定字段(Rly,即nvarchar)放在表的第五个定义字段中(preTotal,它是数字)。