Home » SQL & PL/SQL » SQL & PL/SQL » Dynamic SQL
Dynamic SQL [message #38292] Mon, 08 April 2002 08:45 Go to next message
Gloria
Messages: 4
Registered: October 2001
Junior Member
Hi there,

I used dynamic sql in my plsql function. A table name is passed as parameter. Then I build a del_stmt for deleting records from the table. I used execute immdediate to run the del_stmt. However, I got invalid column name error. Could someone please tell me about the error. The following is a brief example about my function:
del_str:='DELETE FROM '||del_tbl||' dt'||
' WHERE dt.f1 = purge_rec.f1||
' AND dt.f2= purge_rec.f2'||
' AND dt.f3= purge_rec.f3;

FOR purge_rec IN purge_list LOOP

EXECUTE IMMEDIATE del_str;
total_row := total_row + 1;

dbms_output.put_line(total_row);
END LOOP;

Thanks in advance.

Gloria
Re: Dynamic SQL [message #38293 is a reply to message #38292] Mon, 08 April 2002 08:49 Go to previous messageGo to next message
Mahesh Rajendran
Messages: 10707
Registered: March 2002
Location: oracleDocoVille
Senior Member
Account Moderator
--- if this a typo...close the single quotes.

' AND dt.f3= purge_rec.f3;
Re: Dynamic SQL [message #38295 is a reply to message #38293] Mon, 08 April 2002 09:57 Go to previous messageGo to next message
Gloria
Messages: 4
Registered: October 2001
Junior Member
It's only a typo in my message not in my actual code.

thanks,

Gloria
Re: Dynamic SQL [message #38300 is a reply to message #38292] Tue, 09 April 2002 00:12 Go to previous messageGo to next message
John R
Messages: 156
Registered: March 2000
Senior Member
I would imagine that the problem is caused by the scope of the execute immediate statement.
Specifically, the purge_rec row is not when the statement is executed, and so it is unable to recognise the columns you specify.

To fix this you need to move the definition of del_str to inside the cursor loop and replace "dt.f1 = purge_rec.f1" with "dt.f1 = '||purge_rec.f1" if the field is a number, or with "dt.f1 = '''||purge_rec.f1||'''' if it is a string.
Re: Dynamic SQL [message #38305 is a reply to message #38292] Tue, 09 April 2002 02:43 Go to previous message
Raj Mathur
Messages: 11
Registered: April 2002
Junior Member
PROCEDURE PROC_TEST_DY (TName VARCHAR2) AS
SQL_STMT VARCHAR2(2000);
BEGIN
SQL_STMT :='INSERT INTO TAB_CLOB(ID,NAME)
SELECT COUNT(*), '||''''||TNAME||''''||'
FROM '||TNAME;
EXECUTE IMMEDIATE SQL_STMT;
COMMIT;
END ;
Previous Topic: ORA-Errors
Next Topic: Physical date
Goto Forum:
  


Current Time: Thu Apr 25 10:35:47 CDT 2024