INSERT UNION POC结果进入tel

时间:2017-01-08 00:46:51

标签: tsql union

我想改变一个proc,所以我可以将结果插入表中。你能指导一下吗?谢谢..

   ALTER proc [dbo].[usp_CtotalPPPP]
   AS
   BEGIN 
   SELECT   CID, Consumer1, Consumer2, datepart(year, getdate()) Year, 'Jan'  Month, [Jan_S] Budget, [JanAct] Act, getdate() 
         FROM            CProgramDetails
   UNION
   SELECT   CID, Consumer1, Consumer2, datepart(year, getdate()) Year, 'Feb' Month, [FEBOCV_Spend] Budget, [FEBAct] Act, getdate() 
        FROM            CProgramDetails
   UNION
   SELECT CID, Consumer1, Consumer2, date part(year, getdate()) Year, 'Mar' Month, [MarOCV_Spend] Budget, [MarAct] Act, getdate() 
        FROM            CProgramDetails
 UNION
 SELECT  CID, Consumer1, Consumer2, datepart(year, getdate()) Year, 'Apr' Month, [AprOCV_Spend] Budget, [AprAct] Act, getdate() 
        FROM            CProgramDetails
 INSERT INTO [dbo].[ABCD123] (---this is failing - I do not know where to add this)

End

1 个答案:

答案 0 :(得分:1)

IF [dbo]。[ABCD123]已存在

INSERT INTO [dbo].[ABCD123]
Select ...
Union
Select ...

IF [dbo]。[ABCD123]不存在,你想在飞行中创建它

SELECT   CID
       , Consumer1
       , Consumer2
       , datepart(year, getdate()) Year
       , 'Jan'  Month
       , [Jan_S] Budget
       , [JanAct] Act
       , getdate() 
 INTO  [dbo].[ABCD123]       --<< only once in the top query
 FROM   CProgramDetails
UNION
SELECT ...
UNION
SELECT