1

my google-fu is failing me. I'm trying to do the following in a more automatic way:

1) select a set of fields from 1 table

select ACCT_ID from MASTER_ACCT where CUST_NBR like '%ABC';

2) use the results of that in a multiple row insert

// for each ACCT_ID in (1)
insert into TOGGLES (FIELD1, FIELD2, FIELD3)
values('abc', '123', ACCT_ID[i]);

Is there a way to execute the 2nd statement for ACCT_ID[i] in each of the ACCT_ID results from the 1st statement?

Andrei
  • 513
  • 2
  • 8
  • 15
  • possible duplicate of https://stackoverflow.com/questions/25969/insert-into-values-select-from – Shammas Dec 08 '17 at 16:39
  • Which [DBMS](https://en.wikipedia.org/wiki/DBMS) product are you using? Postgres? Oracle? "SQL" is just a query language, not the name of a specific database product. –  Dec 08 '17 at 16:55

2 Answers2

1

You would use an INSERT INTO...SELECT statement:

INSERT INTO toggles (field1, field2, field3)
SELECT 'abc', '123', acct_id
FROM master_acct
WHERE cust_nbr LIKE '%ABC';
gmiley
  • 6,531
  • 1
  • 13
  • 25
1

You might use the below syntax

INSERT INTO target_table[()] SELECT ... FROM ...;

find this link for more details.

Shammas
  • 381
  • 1
  • 4
  • 15