I am trying to insert values into one table based on a subquery on another. The problem is I need to combine literals with results from the subquery to make the new rows.
So I tried this
insert into cust_functions
(SELECT 'CODE1', cust_id, 'LWTest', sysdate
FROM customers
WHERE cust_type_cd = 'distributor');
CODE1 and LWTest are the literals I require in columns 1 & 3. Column 2 contains a unique ID from another table, and column 4 is the current date/time.
If I put the literals in quotes, it tries to use these values including the quotes - which fails because column 1 is constrained to a value in another table. (otherwise I would put it in with quotes and do an update to replace them afterwards)
If I leave out the quotes it thinks CODE1 and LWTest are column names and I get 'invalid column name' error.
I've tried using Select in combination with the VALUES keyword and it doesn't like that either -
e.g.
SQL> insert into cust_functions
2 (cust_function_type_cd,
3 cust_id ,
4 user_id ,
5 date_time_chg)
6 Values
7 ('CODE1',
8 SELECT cust_id from customers WHERE cust_type_cd = 'distributor',
9 'LWTest',
10 sysdate);
and I get
SELECT cust_id from customers WHERE cust_type_cd = 'distributor',
*
ERROR at line 8:
ORA-00936: missing expression
Do I have to get the select statement and iterate through each row? If so how do I do this?
So I tried this
insert into cust_functions
(SELECT 'CODE1', cust_id, 'LWTest', sysdate
FROM customers
WHERE cust_type_cd = 'distributor');
CODE1 and LWTest are the literals I require in columns 1 & 3. Column 2 contains a unique ID from another table, and column 4 is the current date/time.
If I put the literals in quotes, it tries to use these values including the quotes - which fails because column 1 is constrained to a value in another table. (otherwise I would put it in with quotes and do an update to replace them afterwards)
If I leave out the quotes it thinks CODE1 and LWTest are column names and I get 'invalid column name' error.
I've tried using Select in combination with the VALUES keyword and it doesn't like that either -
e.g.
SQL> insert into cust_functions
2 (cust_function_type_cd,
3 cust_id ,
4 user_id ,
5 date_time_chg)
6 Values
7 ('CODE1',
8 SELECT cust_id from customers WHERE cust_type_cd = 'distributor',
9 'LWTest',
10 sysdate);
and I get
SELECT cust_id from customers WHERE cust_type_cd = 'distributor',
*
ERROR at line 8:
ORA-00936: missing expression
Do I have to get the select statement and iterate through each row? If so how do I do this?