【问题标题】:insert the result into a table将结果插入表中
【发布时间】:2016-12-26 18:36:05
【问题描述】:

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

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%')

当我将其保留为子查询时,它会在声明时抛出错误。

请多多指教。

【问题讨论】:

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


    【解决方案1】:
    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%')
    

    【讨论】:

    • 这只会使用一次,但我需要根据我的要求多次插入同一个表
    猜你喜欢
    • 2014-10-20
    • 1970-01-01
    • 1970-01-01
    • 2014-04-29
    • 2018-04-05
    • 2021-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多