日期:2014-05-18  浏览次数:20527 次

用结果集中数据新建表
我在数据库中用联接查询几张表的数据。然后用这个结果集的所有字段并新建一个表,把查询出的所有数据放到这个新建的表中,
请问这个应该怎么写个存储过程?
select  
agmt.Agmt_Id   as   帐户号,
agmt.Agmt_Holder   as   客户号,
limt.Amt_Limit   as   控制额度,
agmt.Open_Dt   as   开户日,
acct.Start_Dt   as   签约日,
acct.Record_Stat   as   签约状态,
hps.yngname   as   机构   ,
hps.fehname   as   分行
from   T03_Agmt_Deposit_Acct_xdck   as   acct   right   outer   join   t03_agmt_xdck   as   agmt
on   (agmt.Agmt_Id   =   acct.Agmt_Id)
right   outer   join   T03_Agmt_Limit_H_Xdck   as   limt   on   (agmt.Agmt_Id   =   limt.Agmt_Id)
right   outer   join   t03_agmt_xdck   as   xdck1   on(acct.Agmt_Id   =   xdck1.Agmt_Id)
left   outer   join   hps_report_rjgjg   as   hps   on   (xdck1.Open_Org   =   hps.yngyjg)
where   acct.Record_Stat= '0 '
这个语句可以查询出很多数据,然后我想以这些数据新建一张表。
请句这个存储过程怎么写?谢谢!!!

------解决方案--------------------
select
agmt.Agmt_Id as 帐户号,
agmt.Agmt_Holder as 客户号,
limt.Amt_Limit as 控制额度,
agmt.Open_Dt as 开户日,
acct.Start_Dt as 签约日,
acct.Record_Stat as 签约状态,
hps.yngname as 机构 ,
hps.fehname as 分行

into 新表名

from T03_Agmt_Deposit_Acct_xdck as acct right outer join t03_agmt_xdck as agmt
on (agmt.Agmt_Id = acct.Agmt_Id)
right outer join T03_Agmt_Limit_H_Xdck as limt on (agmt.Agmt_Id = limt.Agmt_Id)
right outer join t03_agmt_xdck as xdck1 on(acct.Agmt_Id = xdck1.Agmt_Id)
left outer join hps_report_rjgjg as hps on (xdck1.Open_Org = hps.yngyjg)
where acct.Record_Stat= '0 '

------解决方案--------------------
--用查询结果数据生成新表的格式

select *
into 新表名
from 表名
------解决方案--------------------
create proc test
as
if object_id( '新表 ') is not null
drop table 新表
select
agmt.Agmt_Id as 帐户号,
agmt.Agmt_Holder as 客户号,
limt.Amt_Limit as 控制额度,
agmt.Open_Dt as 开户日,
acct.Start_Dt as 签约日,
acct.Record_Stat as 签约状态,
hps.yngname as 机构 ,
hps.fehname as 分行
into 新表
from T03_Agmt_Deposit_Acct_xdck as acct right outer join t03_agmt_xdck as agmt
on (agmt.Agmt_Id = acct.Agmt_Id)
right outer join T03_Agmt_Limit_H_Xdck as limt on (agmt.Agmt_Id = limt.Agmt_Id)
right outer join t03_agmt_xdck as xdck1 on(acct.Agmt_Id = xdck1.Agmt_Id)
left outer join hps_report_rjgjg as hps on (xdck1.Open_Org = hps.yngyjg)
where acct.Record_Stat= '0 '
go
------解决方案--------------------
select
agmt.Agmt_Id as 帐户号,
agmt.Agmt_Holder as 客户号,
limt.Amt_Limit as 控制额度,
agmt.Open_Dt as 开户日,
acct.Start_Dt as 签约日,
acct.Record_Stat as 签约状态,
hps.yngname as 机构 ,
hps.fehname as 分行
Into NewTableName
from T03_Agmt_Deposit_Acct_xdck as acct right outer join t03_agmt_xdck as agmt
on (agmt.Agmt_Id = acct.Agmt_Id)
right outer join T03_Agmt_Limit_H_Xdck as limt on (agmt.Agmt_Id = limt.Agmt_Id)
right outer join t03_agmt_xdck as xdck1 on(acct.Agmt_Id = xdck1.Agmt_Id)
left outer join hps_report_rjgjg as hps on (xdck1.Open_Org = hps.yngyjg)
where acct.Record_Stat= '0 '

------解决方案--------------------
create proc ZZ as
insert into New_T
select agmt.Agmt_Id as 帐户号,
agmt.Agmt_Holder as 客户号,