Tsql 从另一个查询填充表

Tsql 从另一个查询填充表,tsql,sql-server-2008,insert,Tsql,Sql Server 2008,Insert,因此,我有一个SQL查询,它为我提供了一个整数列表。此处列出: select distinct re_entity_id from cfo_transaction inner join cfo_tran_quote ON tq_tr_transaction_id = tr_transaction_id inner join cfo_trans_entity_rel on te_tr_transaction_i

因此,我有一个SQL查询,它为我提供了一个整数列表。此处列出:

select distinct
            re_entity_id
      from cfo_transaction 
            inner join cfo_tran_quote  ON tq_tr_transaction_id = tr_transaction_id
            inner join cfo_trans_entity_rel on te_tr_transaction_id = tr_transaction_id and te_rv_rel_type_id in (713,715)
            inner join com_entity on te_co_re_entity_id = re_entity_id
      where 
            dbo.islmsloan(tq_tran_quote_id) = 1
            and isnull(re_fictitious_bit,0) = 0
这给了我一个id列表,我需要将它和其他东西一起插入到另一个表中。另一个表如下所示:

ens_engine_sponsor_id - PK
ens_rs_sponsor_id - relates to the id from the other query
ens_use_new_models_bit - should always be 1 for each insert
ens_start_dt - should be 09/05/2011 for every one
ens_end_dt - should be null for every one
我该如何制定一些公式,在给定的条件下,自动为这个新表中的每个Id插入一行?(SQL不太好…)


谢谢

您可以将常量添加到
选择列表中,如下所示

insert into othertable
            (ens_rs_sponsor_id,
             ens_use_new_models_bit,
             ens_start_dt)
select distinct re_entity_id,
                1,
                '20110905'
from   cfo_transaction
       inner join cfo_tran_quote
         ON tq_tr_transaction_id = tr_transaction_id
       inner join cfo_trans_entity_rel
         on te_tr_transaction_id = tr_transaction_id
            and te_rv_rel_type_id in ( 713, 715 )
       inner join com_entity
         on te_co_re_entity_id = re_entity_id
where  dbo.islmsloan(tq_tran_quote_id) = 1
       and isnull(re_fictitious_bit, 0) = 0  

如果使用<代码>区分< /代码>是删除连接所带来的副本,则可以考虑使用<>代码>存在的。

< P>可以将常量添加到<代码> >选择< /COD>列表如下。

insert into othertable
            (ens_rs_sponsor_id,
             ens_use_new_models_bit,
             ens_start_dt)
select distinct re_entity_id,
                1,
                '20110905'
from   cfo_transaction
       inner join cfo_tran_quote
         ON tq_tr_transaction_id = tr_transaction_id
       inner join cfo_trans_entity_rel
         on te_tr_transaction_id = tr_transaction_id
            and te_rv_rel_type_id in ( 713, 715 )
       inner join com_entity
         on te_co_re_entity_id = re_entity_id
where  dbo.islmsloan(tq_tran_quote_id) = 1
       and isnull(re_fictitious_bit, 0) = 0  

如果使用<代码>不同的< /代码>是删除连接所带来的副本,则可以考虑使用<>代码>存在的。

您没有提及EnnSnEngEngPosialId是一个标识字段,但假设它是可以做到这一点的。
    INSERT INTO MyTableName(
        ens_rs_sponsor_id,      
        ens_use_new_models_bit,
        ens_start_dt,
        ens_end_dt)
select distinct 
            re_entity_id,
            1,
            '09 May 2011',
            NULL
      from cfo_transaction  
            inner join cfo_tran_quote  ON tq_tr_transaction_id = tr_transaction_id 
            inner join cfo_trans_entity_rel on te_tr_transaction_id = tr_transaction_id and te_rv_rel_type_id in (713,715) 
            inner join com_entity on te_co_re_entity_id = re_entity_id 
      where  
            dbo.islmsloan(tq_tran_quote_id) = 1 
            and isnull(re_fictitious_bit,0) = 0 

您没有提到ens_engine_Consponder_id是否是一个标识字段,但假设是,您可以这样做

    INSERT INTO MyTableName(
        ens_rs_sponsor_id,      
        ens_use_new_models_bit,
        ens_start_dt,
        ens_end_dt)
select distinct 
            re_entity_id,
            1,
            '09 May 2011',
            NULL
      from cfo_transaction  
            inner join cfo_tran_quote  ON tq_tr_transaction_id = tr_transaction_id 
            inner join cfo_trans_entity_rel on te_tr_transaction_id = tr_transaction_id and te_rv_rel_type_id in (713,715) 
            inner join com_entity on te_co_re_entity_id = re_entity_id 
      where  
            dbo.islmsloan(tq_tran_quote_id) = 1 
            and isnull(re_fictitious_bit,0) = 0 

2011年5月9日是5月9日还是9月5日?2011年5月9日是5月9日还是9月5日?