我要運行該SP,但它不工作,並給予誤差添加的選擇計數的臨時表
是分配一個值的變量不能與數據檢索相結合的SELECT語句操作。
ALTER PROCEDURE GET_RECORDS_FORDATE
@From_date Datetime,
@To_date Datetime
AS
BEGIN
SELECT *
INTO #temp
FROM (
select 0 SR_NO, 0 date, 0 Total, 0 Inward, 0 First_Level_Transfer, 0 Data_Entry_Transfer,
0 Second_Level_Transfer, 0 Outward_Transfer,
0 Closed, 0 Communication_Transfer
from inward_doc_tracking_hdr
) AS x
select count(*) tot_count,'Total' stage from inward_doc_tracking_hdr
where
doc_date between convert(datetime, @From_date ,103)
and convert(datetime, @To_date, 103)
select count(*) tot_count,'Inward' stage from inward_doc_tracking_hdr
where
doc_date between convert(datetime, '24/05/2016',103)
and convert(datetime, '26/05/2016', 103)
and status_flag in ('6')
select count(*) tot_count,'1st Level Transfer' stage from inward_doc_tracking_hdr
where doc_date between convert(datetime, '24/05/2016',103)
and convert(datetime, '26/05/2016', 103)
and status_flag in ('4','26','24')
select count(*) tot_count,'Data Entry Transfer' stage from inward_doc_tracking_hdr
where doc_date between convert(datetime, '24/05/2016',103)
and convert(datetime, '26/05/2016', 103)
and status_flag in ('15','20')
select count(*) tot_count,'2nd Level Transfer' stage from inward_doc_tracking_hdr
where doc_date >=convert(datetime,'24/05/2016',103)
and status_flag in ('17','21')
select count(*) tot_count,'Outward Transfer' stage from inward_doc_tracking_hdr
where doc_date >=convert(datetime,'24/05/2016',103)
and status_flag='18'
select count(*) tot_count,'Close' stage from inward_doc_tracking_hdr
where doc_date between convert(datetime, '24/05/2016',103)
and convert(datetime, '26/05/2016', 103)
and status_flag='5'
select count(*) tot_count,'Communication Transfer' stage from inward_doc_tracking_hdr
where doc_date between convert(datetime, '24/05/2016',103)
and convert(datetime, '26/05/2016', 103)
and status_flag='16'
END
SELECT * FROM #temp
END
我想伯爵與SR_NO和日期添加爲每列臨時表。
注意對於目前我想添加日期24之間 - 我使用sql-server-2005
'選擇@Total = COUNT(*),「合計」階段,...' - 那裏正試圖分配一個值的第一部分一個變量(如錯誤消息所述),下一部分將嘗試*返回*列值作爲結果集(數據檢索)的一部分。你不能在一個'SELECT'中做這兩件事。 –
@Damien_The_Unbeliever:我在幾分鐘內更新我的問題作爲我想要的..讓我更新它,可能然後你會得到一個想法我想要什麼 – BNN
你應該在@ SELECT語句中分配@Total變量 – Petaflop