我创建了一个可以工作并提供我想要的查询,但是它不会在批处理文件中执行。如何使用成功执行的查询的结果填充另一个表?
这是我的查询
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
答案 0 :(得分:0)
有两种方法可以处理它:
CREATE TABLE mytable AS
(SELECT ... )
将创建一个包含查询结果的新表格。
INSERT INTO mytable (field1, field2, ...)
(SELECT ... )
会将查询结果插入现有表中。您需要确保列列表自然匹配。如果查询返回的列列表与您现有的表完全匹配,那么您不需要指定字段列表,您的语句就变得简单
INSERT INTO mytable (SELECT ... )
答案 1 :(得分: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已存在并且具有与查询返回的数据类型相同的列。