我有一个SP,它正常工作,直到我添加了两个列。现在,在添加了两列后,它开始给出错误
INSERT语句的选择列表包含的项目少于插入列表。 SELECT值的数量必须与INSERT列的数量匹配。
但我已经插入了这些列。
两个新添加的列
Dept_received varchar(110),doc_type varchar(110)
以下是我的SP。
ALTER PROCEDURE [dbo].[Inward_Rec_Dept_doc]
@From_date Datetime,
@To_date Datetime
AS
BEGIN
CREATE TABLE #temp(
Dept_received varchar(110), doc_type varchar(110), date datetime, Total int,doc_From_To varchar(50),Inward int, First_Level_Transfer int,
Data_Entry_Transfer int,
Second_Level_Transfer int, Outward_Transfer int,
Closed int, Communication_Transfer int, Returned int
)
INSERT INTO #temp
(Dept_received, doc_type, date, Total,doc_From_To, Inward, First_Level_Transfer,
Data_Entry_Transfer,
Second_Level_Transfer, Outward_Transfer,
Closed, Communication_Transfer, Returned)
SELECT
doc_date, COUNT(*),
(select kk.doc_no FROM inward_doc_tracking_hdr kk where mkey in (select min(mkey) FROM inward_doc_tracking_hdr jj
where jj.doc_date =convert(datetime,aa.doc_date,103) ) )
+ ' - '+
(select kk.doc_no FROM inward_doc_tracking_hdr kk where mkey in (select max(mkey) FROM inward_doc_tracking_hdr jj
where jj.doc_date =convert(datetime,aa.doc_date,103) ) )
,SUM(
CASE
WHEN status_flag in ('6','23') THEN 1 ELSE 0
END)
,SUM(
CASE
WHEN status_flag in ('4','26','24') THEN 1 ELSE 0
END)
,SUM(
CASE
WHEN status_flag in ('15','20') THEN 1 ELSE 0
END),
SUM(
CASE
WHEN status_flag in ('17','21') THEN 1 ELSE 0
END),
SUM(
CASE
WHEN status_flag in ('18','27') THEN 1 ELSE 0
END),
SUM(
CASE
WHEN status_flag='5' THEN 1 ELSE 0
END),
SUM(
CASE
WHEN status_flag='16' THEN 1 ELSE 0
END),
SUM(
CASE
WHEN status_flag='14' THEN 1 ELSE 0
END)
FROM inward_doc_tracking_hdr aa
WHERE doc_date between @From_date and @To_date
--AND status_flag <> '6'
GROUP BY doc_date, Dept_received, doc_type
END
Select * from #temp
我正在使用sql-server-2005
请帮助我,错误是什么
更新后的查询
ALTER PROCEDURE [dbo].[Inward_Rec_Dept_doc]
@From_date Datetime,
@To_date Datetime
AS
BEGIN
CREATE TABLE #temp(
Dept_received datetime,
doc_type varchar(110), date datetime, Total int,doc_From_To varchar(50),
Inward int, First_Level_Transfer int,
Data_Entry_Transfer int,
Second_Level_Transfer int, Outward_Transfer int,
Closed int, Communication_Transfer int, Returned int
)
INSERT INTO #temp
(
Dept_received,
doc_type,
date,
Total,
doc_From_To,
Inward,
First_Level_Transfer,
Data_Entry_Transfer,
Second_Level_Transfer,
Outward_Transfer,
Closed,
Communication_Transfer,
Returned
)
SELECT
doc_date, COUNT(*),
(select kk.doc_no FROM inward_doc_tracking_hdr kk where mkey in (select min(mkey) FROM inward_doc_tracking_hdr jj
where jj.doc_date =convert(datetime,aa.doc_date,103) ) )
+ ' - '+
(select kk.doc_no FROM inward_doc_tracking_hdr kk where mkey in (select max(mkey) FROM inward_doc_tracking_hdr jj
where jj.doc_date =convert(datetime,aa.doc_date,103) ) )
,SUM(
CASE
WHEN status_flag in ('6','23') THEN 1 ELSE 0
END)
,SUM(
CASE
WHEN status_flag in ('4','26','24') THEN 1 ELSE 0
END)
,SUM(
CASE
WHEN status_flag in ('15','20') THEN 1 ELSE 0
END),
SUM(
CASE
WHEN status_flag in ('17','21') THEN 1 ELSE 0
END),
SUM(
CASE
WHEN status_flag in ('18','27') THEN 1 ELSE 0
END),
SUM(
CASE
WHEN status_flag='5' THEN 1 ELSE 0
END),
SUM(
CASE
WHEN status_flag='16' THEN 1 ELSE 0
END),
SUM(
CASE
WHEN status_flag='14' THEN 1 ELSE 0
END),1,1
FROM inward_doc_tracking_hdr aa
WHERE doc_date between @From_date and @To_date
--AND status_flag <> '6'
GROUP BY doc_date, dept_received, doc_type
END
Select * from #temp
答案 0 :(得分:1)
ALTER PROCEDURE [dbo].[Inward_Rec_Dept_doc]
@From_date Datetime,
@To_date Datetime
AS
BEGIN
CREATE TABLE #temp(
Dept_received varchar(110),
doc_type varchar(110), date datetime, Total int,doc_From_To varchar(50),
Inward int, First_Level_Transfer int,
Data_Entry_Transfer int,
Second_Level_Transfer int, Outward_Transfer int,
Closed int, Communication_Transfer int, Returned int
)
INSERT INTO #temp
(Dept_received, doc_type, date, Total,doc_From_To, Inward, First_Level_Transfer,
Data_Entry_Transfer,
Second_Level_Transfer, Outward_Transfer,
Closed, Communication_Transfer, Returned)
SELECT
doc_date, COUNT(*),
(select kk.doc_no FROM inward_doc_tracking_hdr kk where mkey in (select min(mkey) FROM inward_doc_tracking_hdr jj
where jj.doc_date =convert(datetime,aa.doc_date,103) ) )
+ ' - '+
(select kk.doc_no FROM inward_doc_tracking_hdr kk where mkey in (select max(mkey) FROM inward_doc_tracking_hdr jj
where jj.doc_date =convert(datetime,aa.doc_date,103) ) )
,SUM(
CASE
WHEN status_flag in ('6','23') THEN 1 ELSE 0
END)
,SUM(
CASE
WHEN status_flag in ('4','26','24') THEN 1 ELSE 0
END)
,SUM(
CASE
WHEN status_flag in ('15','20') THEN 1 ELSE 0
END),
SUM(
CASE
WHEN status_flag in ('17','21') THEN 1 ELSE 0
END),
SUM(
CASE
WHEN status_flag in ('18','27') THEN 1 ELSE 0
END),
SUM(
CASE
WHEN status_flag='5' THEN 1 ELSE 0
END),
SUM(
CASE
WHEN status_flag='16' THEN 1 ELSE 0
END),
SUM(
CASE
WHEN status_flag='14' THEN 1 ELSE 0
END),1,1 --Add your values in place of 1, your issue will be resolved
FROM inward_doc_tracking_hdr aa
WHERE doc_date between @From_date and @To_date
--AND status_flag <> '6'
GROUP BY doc_date, Dept_received, doc_type
END
Select * from #temp
答案 1 :(得分:0)
使用INSERT
可以省去将SELECT INTO
ed列数和类型与临时表结构对齐的问题:
ALTER PROCEDURE [dbo].[Inward_Rec_Dept_doc]
@From_date Datetime,
@To_date Datetime
AS
BEGIN
IF OBJECT_ID('tempdb..#temp') IS NOT NULL DROP TABLE #temp;
SELECT
doc_date As [date], dept_received, doc_type, COUNT(*) As Total
,(SELECT kk.doc_no FROM inward_doc_tracking_hdr kk WHERE mkey in (SELECT min(mkey) FROM inward_doc_tracking_hdr jj WHERE jj.doc_date = convert(datetime,aa.doc_date,103) ) )
+ ' - '+
(SELECT kk.doc_no FROM inward_doc_tracking_hdr kk WHERE mkey in (SELECT max(mkey) FROM inward_doc_tracking_hdr jj WHERE jj.doc_date = convert(datetime,aa.doc_date,103) ) )
As doc_From_To
,SUM(CASE WHEN status_flag in ( '6', '23') THEN 1 ELSE 0 END) As Inward
,SUM(CASE WHEN status_flag in ( '4', '26', '24') THEN 1 ELSE 0 END) As First_Level_Transfer
,SUM(CASE WHEN status_flag in ('15', '20') THEN 1 ELSE 0 END) As Data_Entry_Transfer
,SUM(CASE WHEN status_flag in ('17', '21') THEN 1 ELSE 0 END) As Second_Level_Transfer
,SUM(CASE WHEN status_flag in ('18', '27') THEN 1 ELSE 0 END) As Outward_Transfer
,SUM(CASE WHEN status_flag = '5' THEN 1 ELSE 0 END) As Closed
,SUM(CASE WHEN status_flag = '16' THEN 1 ELSE 0 END) As Communication_Transfer
,SUM(CASE WHEN status_flag = '14' THEN 1 ELSE 0 END) As Returned
INTO #temp
FROM inward_doc_tracking_hdr aa
WHERE doc_date between @From_date and @To_date
--AND status_flag <> '6'
GROUP BY doc_date, dept_received, doc_type
END
GO
Select * from #temp;