2016-12-26 79 views
-1

我需要時,我用它來保持這個作爲子查詢它在聲明拋出一個錯誤插入這個結果在一個表中結果插入到表

declare @i int 

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

select ROW_NUMBER() OVER (ORDER BY DateTime_Executed)[email protected] 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%') 

懇請指正。

回答

0
select ROW_NUMBER() OVER (ORDER BY DateTime_Executed)[email protected] 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)[email protected] 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%') 
+0

這將只能使用一次,但我需要插入到同一個表上多次按我的要求 –