So I have a SQL query that gives me a list of ints. Listed here:
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
This gives me a list of id's that I need to insert into another table along with other stuff. The other table looks like this:
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
How would I开发者_StackOverflow formulate something that automatically inserts a row for each of those Id's in this new table with the given criteria? (Not so good with SQL...)
Thanks
You can add constants into your SELECT
list as below.
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
If the reason for using DISTINCT
is to remove duplicates brought in by the joins you could consider using WHERE EXISTS
instead.
You didn't mention whether ens_engine_sponsor_id is an identity field but assuming it is then you can do this.
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
精彩评论