0

我创建了一个可以工作并提供我想要的查询,但是它不会在批处理文件中执行。如何使用成功执行的查询的结果填充另一个表?

这是我的查询

select  

isnull (start_charge.account,'No account at start') Start_charge_account
,Finish_charge.account start_charge_account   
,case when start_charge.account is null then finish_charge.account else       start_charge.account end "account" 
,isnull (start_charge.Branch,99999999999) Start_charge_Branch
,Finish_charge.Branch Finish_charge_Branch
,case when start_charge.branch is null then finish_charge.branch else  start_charge.branch end " branch"
,isnull (start_charge. site_number,99999999999) Start_charge_site_number
,Finish_charge. site_number Finish_charge_site_number
,case when start_charge.site_number is null then finish_charge.site_number else start_charge.site_number end "site_number"
,isnull (start_charge. service_code, 'No account at start') Start_charge_service_code
,Finish_charge. service_code finish_charge_service_code
,isnull (start_charge. item_code,'No account at start') Start_charge_item_code
,Finish_charge. item_code finish_charge_item_code
,isnull (start_charge. why_code, 'No account at start') Start_charge_why_code
,Finish_charge. why_code finish_charge_why_code
,isnull (start_charge. why_type, 'No account at start') Start_charge_why_type
,Finish_charge. why_type finish_charge_why_type

,Start_charge.Start_charge
,isnull(Finish_charge.Finish_charge,0) Finish_charge
,isnull(Start_charge.Start_charge,0)-isnull(Finish_charge.Finish_charge,0) Variance


from

(
select

account
,branch
,site_number
,service_code
,item_code
,why_code
,why_type
,sum(charge)
 "Start_Charge"

from

cannon_commercial

where

1=1
and record_type = 'PS'
and period = ' 02/07/2013'

group by

account
,branch
,site_number
,service_code
,item_code
,why_code
,why_type
) start_charge 



    full outer join 



( select

account
,branch
,site_number
,service_code
,item_code
,why_code
,why_type
,concat(account,site_number) "account - site"
,ISNULL(sum(charge),0) "Finish_Charge"

from

cannon_commercial

where

1=1
and record_type= 'PS'
and period = ' 04/06/2013'

group by

account
,branch
,site_number
,service_code
,item_code
,why_code
,why_type
)
Finish_charge

on  Start_charge.Account = Finish_charge.Account
and Start_charge.Branch = Finish_charge.Branch
and Start_charge.site_number = Finish_charge.site_number
and Start_charge.item_code = Finish_charge.item_code
and Start_charge.service_code = Finish_charge.service_code
and Start_charge.why_code = Finish_charge.why_code
and Start_charge.why_type = Finish_charge.why_type
4

2 回答 2

0

是的,您可以填充与查询返回的列数和数据类型相同的现有表。

您的查询可以是

INSERT INTO TABLE_NAME

(
select  

isnull (start_charge.account,'No account at start') Start_charge_account
,Finish_charge.account start_charge_account   
,case when start_charge.account is null then finish_charge.account else       start_charge.account end "account" 
,isnull (start_charge.Branch,99999999999) Start_charge_Branch
,Finish_charge.Branch Finish_charge_Branch
,case when start_charge.branch is null then finish_charge.branch else  start_charge.branch end " branch"
,isnull (start_charge. site_number,99999999999) Start_charge_site_number
,Finish_charge. site_number Finish_charge_site_number
,case when start_charge.site_number is null then finish_charge.site_number else start_charge.site_number end "site_number"
,isnull (start_charge. service_code, 'No account at start') Start_charge_service_code
,Finish_charge. service_code finish_charge_service_code
,isnull (start_charge. item_code,'No account at start') Start_charge_item_code
,Finish_charge. item_code finish_charge_item_code
,isnull (start_charge. why_code, 'No account at start') Start_charge_why_code
,Finish_charge. why_code finish_charge_why_code
,isnull (start_charge. why_type, 'No account at start') Start_charge_why_type
,Finish_charge. why_type finish_charge_why_type

,Start_charge.Start_charge
,isnull(Finish_charge.Finish_charge,0) Finish_charge
,isnull(Start_charge.Start_charge,0)-isnull(Finish_charge.Finish_charge,0) Variance


from

(
select

account
,branch
,site_number
,service_code
,item_code
,why_code
,why_type
,sum(charge)
 "Start_Charge"

from

cannon_commercial

where

1=1
and record_type = 'PS'
and period = ' 02/07/2013'

group by

account
,branch
,site_number
,service_code
,item_code
,why_code
,why_type
) start_charge 



    full outer join 



( select

account
,branch
,site_number
,service_code
,item_code
,why_code
,why_type
,concat(account,site_number) "account - site"
,ISNULL(sum(charge),0) "Finish_Charge"

from

cannon_commercial

where

1=1
and record_type= 'PS'
and period = ' 04/06/2013'

group by

account
,branch
,site_number
,service_code
,item_code
,why_code
,why_type
)
Finish_charge

on  Start_charge.Account = Finish_charge.Account
and Start_charge.Branch = Finish_charge.Branch
and Start_charge.site_number = Finish_charge.site_number
and Start_charge.item_code = Finish_charge.item_code
and Start_charge.service_code = Finish_charge.service_code
and Start_charge.why_code = Finish_charge.why_code
and Start_charge.why_type = Finish_charge.why_type
)

确保表 TABLE_NAME 已经存在并且具有与查询返回的数据类型相同的列。

于 2013-09-11T09:30:45.500 回答
0

有两种处理方法:

CREATE TABLE mytable AS
  (SELECT ... )

将使用您的查询结果创建一个新表。

INSERT INTO mytable (field1, field2, ...)
  (SELECT ... )

会将您的查询结果插入到现有表中。您需要确保列列表自然匹配。如果查询返回的列列表与您现有的表完全匹配,那么您不需要指定字段列表并且您的语句变得简单

INSERT INTO mytable (SELECT ... )
于 2013-09-11T09:21:14.350 回答