1

I tried to make the table name here dynamic and it didn't insert any records to the table. Can someone help me take a look what is wrong? Thank you! '''

Create or replace procedure input_records AS

CurTerm varchar2(4) := '1122';
CAR_TERM_HISTORY varchar2(30) := 'CAR_TERM_HISTORY_' || CurTerm;
stmt_str varchar2(1000) := 'insert into' || CAR_TERM_HISTORY
|| '(select sysdate as date_created,
date_loaded,
strm,
withdraw_code,
withdraw_reason,
withdraw_date,
unt_taken_fa,
unt_passd_fa,
tot_taken_fa,
tot_passd_fa,
tot_taken_fa_gpa,
tot_grd_points_fa,
billing_career,
cur_gpa,
cum_gpa,
registered,
acad_level_proj,
acad_level_bot,
acad_level_eot '||
'from IMS.EADTERM
where strm = CurTerm
)';


BEGIN
execute immediate stmt_str ;

END input_records;

'''

3
  • 2
    The string you're assembling has syntax errors. So I assume you're getting an error when you call the procedure. Including the error in your question would be useful. If you want to use dynamic SQL, always always always write it so that you are logging/ printing/ saving the SQL that you've dynamically assembled before you try to run it. That will make it far, far easier to debug. If you print out the SQL statement before you try to run it and tried to run that separately, the first error you'd get would be that there is no space between into and the table name. Commented Oct 2, 2021 at 4:24
  • 2
    You are missing a space; it should be 'insert into '||CAR_TERM_HISTORY and not 'insert into'||CAR_TERM_HISTORY. Also, where strm = CurTerm may want to be where strm = '||CurTerm||'.
    – MT0
    Commented Oct 2, 2021 at 9:44
  • @MT0 You are right. Thanks for other suggestions!
    – Kasu
    Commented Oct 2, 2021 at 12:29

3 Answers 3

1
where strm = CurTerm

I assume your CurTerm is supposed to be some sort of variable? Do you mean:

where strm = ' || CurTerm || '...

When building SQL like this, make sure that it's not possible to do a SQL injection.

2
  • Yes @eaolson CurTerm is a variable assigned at the beginning. When I changed to ' acad_level_bot, acad_level_eot from IMS.EADTERM where strm = '|| CurTerm ||' )'; ' Still not working.
    – Kasu
    Commented Oct 2, 2021 at 2:07
  • Does "not working" mean you get an error message? Commented Oct 2, 2021 at 8:33
0

You need to change your where condition to -

'from IMS.EADTERM
where strm = ' || CurTerm || '
)';
0

Thanks for everyone's comments. I missed a space after the insert into as @ MT0 pointed out.

1
  • 1
    this is not an answer, should be in the comments sectiion. Commented Oct 2, 2021 at 14:12

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.