2011-08-31 3 views
1

그래서 int 목록을 제공하는 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 

. 다른 테이블은 다음과 같습니다

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 

가 어떻게 자동으로 부여 기준이 새 테이블에있는 그 아이디의 각 행을 삽입 뭔가를 공식화하는 것? (SQL에 그리 좋은 ...)

감사

+0

09/05/2011은 9 월 5 일 또는 9 월 5 일입니까? –

+0

9 월 5 일 ... 좋은 전화 – slandau

답변

3

당신은 다음과 같이 당신의 SELECT 목록에 상수를 추가 할 수 있습니다.

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 

DISTINCT를 사용하는 이유는 당신이 대신 WHERE EXISTS를 사용하여 고려할 수 조인에 의해 가져온 중복을 제거하는 것입니다 경우.

+0

고마워요. 이것은 좋아 보인다. – slandau

1

ens_engine_sponsor_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 
관련 문제