簡體   English   中英

使用存儲過程將數據插入兩個臨時表中

[英]Insert data into two temporary tables using a stored procedure

我有一個存儲過程,其數據如下所示

存儲過程

現在我想要的是,我想將其數據插入兩個不同的臨時表中

第一個臨時表將具有這些列

Doc_type, Doc_No, No_of_days

第二個臨時表將具有列

Username, DocType, No_of_days

我這樣嘗試過:

CREATE TABLE #table1 
(
    Doc_type varchar(55),
    Doc_No varchar(55),
    No_of_days varchar(55),
)

INSERT INTO #table1 

但這會引發錯誤:

關鍵字“ END”附近的語法不正確。

這是我完整的存儲過程:

Alter procedure GET_INWARD_REMINDER_REPORT 
AS 
BEGIN 
    Select
        U.first_name + ' ' + U.last_name UserName, 
        TH.User_ID, 
        TY.Type_desc Document_Type, 
        RA.mkey Reporting_To,
        U.Email AS UserEmail, 
        RAU.Email AS RA1_Email, 
        RAU.first_name + ' ' + RAU.last_name RAName, 
        TH.Doc_No, 
        DATEDIFF(DAY, TH.LastAction_DateTime, GETDATE()) - DATEDIFF(WK, TH.LastAction_DateTime, GETDATE()) AS No_Of_Days_Doc_Pending 
    from  
        inward_doc_tracking_hdr TH 
    inner join  
        user_mst U ON TH.User_Id = U.mkey 
    inner join 
        emp_mst M ON M.mkey = U.employee_mkey 
    inner join 
        type_mst_a TY ON TY.master_mkey = TH.doc_type 
    inner join 
        emp_mst RA ON RA.mkey = M.Reporting_To 
    inner join  
        user_mst RAU ON RAU.employee_mkey = RA.mkey 
    where 
        TH.Status_flag NOT IN (5,14)  --- 5 for close, 14 for return
        and TH.To_user IS NOT NULL 

CREATE TABLE #table1 
(   
    Doc_type varchar(55),
    Doc_No varchar(55),
    No_of_days varchar(55),
)

INSERT INTO #table1 

獲取所有數據到臨時表,然后插入。

INSERT INTO #MainTemp
EXEC [GET_INWARD_REMINDER_REPORT];

INSERT INTO #table1
select Doc_type, Doc_No, No_of_days from #MainTemp

INSERT INTO #table2
select Username, DocType, No_of_days from #MainTemp

更新

您可以使用MainTempTable

Alter procedure GET_INWARD_REMINDER_REPORT 
AS 
BEGIN 

IF EXISTS(SELECT * FROM   dbo.MainTempTable) 
  DROP TABLE dbo.MainTempTable

Select 
U.first_name + ' ' + U.last_name UserName, 
TH.User_ID, 
TY.Type_desc Document_Type, 
RA.mkey Reporting_To, 
U.Email AS UserEmail, 
RAU.Email AS RA1_Email, 
RAU.first_name + ' ' + RAU.last_name RAName, 
TH.Doc_No, 
DATEDIFF(DAY,TH.LastAction_DateTime,GETDATE()) - DATEDIFF(WK,TH.LastAction_DateTime, GETDATE()) 
AS No_Of_Days_Doc_Pending 

INTO MainTempTable

from inward_doc_tracking_hdr TH 
inner join  
user_mst U ON TH.User_Id = U.mkey 
inner join 
emp_mst M ON M.mkey = U.employee_mkey 
inner join 
type_mst_a TY ON TY.master_mkey = TH.doc_type 
inner join 
emp_mst RA ON RA.mkey = M.Reporting_To 
inner join  
user_mst RAU ON RAU.employee_mkey = RA.mkey 
where 
TH.Status_flag NOT IN (5,14) --- 5 for close, 14 for return 
and TH.To_user IS NOT NULL 

SELECT 
   userName, 
   COUNT(Doc_No) CountofDocNo
FROM 
   MainTempTable 
GROUP BY 
   userName

END 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM