将结果插入表中

时间:2016-12-26 18:36:05

标签: sql sql-server sql-server-2008 sql-server-2012

我需要将此结果插入一个表

declare @i int

set @i=(select max(id) from finalaccountdetails)
(

select ROW_NUMBER()  OVER (ORDER BY DateTime_Executed )+@i as id ,

 DateTime_Executed, ReportName,Region, 'IM Account' as Account_Name_Level_1,'IM' as Management_Group_Name, 
 replace(
       replace(C
       ,'%5c','\')
      ,'%2B','+') as Account_code_level_1 ,count(*) as NumberofExecution from preaccountdetails

group by DateTime_Executed, ReportName,Region,c
having c like '%-%' or c like '%2B%')

当我使用它作为子查询时,它会在声明时抛出错误。

善意的建议。

1 个答案:

答案 0 :(得分:0)

select ROW_NUMBER()  OVER (ORDER BY DateTime_Executed )+@i as id ,

 DateTime_Executed, ReportName,Region, 'IM Account' as Account_Name_Level_1,'IM' as Management_Group_Name, 
 replace(
       replace(C
       ,'%5c','\')
      ,'%2B','+') as Account_code_level_1 ,count(*) as NumberofExecution
into #TempTbl
 from preaccountdetails

group by DateTime_Executed, ReportName,Region,c
having c like '%-%' or c like '%2B%')

如果你想要它每次添加它。使用select子句的模式创建一个表,使用以下查询:

   insert into [Newtable]
   select ROW_NUMBER()  OVER (ORDER BY DateTime_Executed )+@i as id ,

     DateTime_Executed, ReportName,Region, 'IM Account' as Account_Name_Level_1,'IM' as Management_Group_Name, 
     replace(
           replace(C
           ,'%5c','\')
          ,'%2B','+') as Account_code_level_1 ,count(*) as NumberofExecution
     from preaccountdetails

    group by DateTime_Executed, ReportName,Region,c
    having c like '%-%' or c like '%2B%')