简体   繁体   中英

Oracle insert statement with sub-query returning more than 1 row

I have a table (L) like so:

L_ID    L_CODE  EFFECTIVE_DATE  EXPIRY_DATE  CLIENT_NUM
1       123     1/1/2014        12/31/2014   ABC
2       123     1/1/2015        6/1/2015     ABC
3       123     6/2/2015        12/31/9999  ABC

I have an insert statement in a PL/SQL package like so:

        INSERT INTO LE (l_email_id,
                                    l_id,
                                    source,
                                    contact_name,
                                    contact_phone,
                                    email_address,
                                    effective_date,
                                    expiry_date,
                                    created_by,
                                    create_timestamp,
                                    updated_by,
                                    update_timestamp)
             VALUES (
                       SEQ_LE.NEXTVAL,
                       (SELECT l_id
                          FROM L
                         WHERE client_num = in_client_num
                           AND UPPER (l_code) = UPPER (in_l_code)
                           AND in_year BETWEEN EXTRACT(YEAR FROM effective_date)
                                                         AND EXTRACT(YEAR FROM expiry_date)),
                       'JURIS',
                       NULL,
                       NULL,
                       TRIM (v_email_address_a (v_index_a)),
                       TRUNC (SYSDATE),
                       (ADD_MONTHS (TRUNC (SYSDATE, 'YEAR'), 12) - 1),
                       (SELECT ad_user
                          FROM OAU
                         WHERE ad_user_id = in_ad_user_id),
                       SYSTIMESTAMP,
                       (SELECT ad_user
                          FROM OAU
                         WHERE ad_user_id = in_ad_user_id),
                       SYSTIMESTAMP);

The sub-query to L table will retrive l_id 's 2 and 3 if in_year is 2015. My question is, how can I insert both l_id 's? There won't always be 2 records, sometimes there could be 1 or more than 2 as well.

When inserting multiple rows like this , the best thing to do is a statement like

 Insert into YourTable (col1,col2,..) select (exp1,exp2,...)  

you can use joins in your select.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM