Home » SQL & PL/SQL » SQL & PL/SQL » Procedure.,
Procedure., [message #608467] Thu, 20 February 2014 03:32 Go to next message
Rarawshenkiv
Messages: 5
Registered: February 2014
Location: Chennai
Junior Member
How to execute this procedure, where I'm passing table name as input?

Create or replace procedure p1( emp_name in varchar2, table_name in varchar2, emp_id in number, o out varchar2)
as
begin
execute immediate 'select'|| emp_name ||'into'|| o ||'from'|| table_name ||'where emp_id='|| emp_id ;
dbms_output.put_line(o);
end;

I can compile this procedure, But while executing, I'm getting error as below:

declare
x varchar2(50);
begin
p1('emp', 'Vicky',5, x);
dbms_output.put_line(x);
end;


Error report:
ORA-00900: invalid SQL statement
ORA-06512: at "PORTALADMIN.P1", line 4
ORA-06512: at line 4
00900. 00000 - "invalid SQL statement"
Re: Procedure., [message #608468 is a reply to message #608467] Thu, 20 February 2014 03:42 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Please read and follow How to use [code] tags and make your code easier to read?

When using dynamic sql you should always display the dynamic string you've built up to check it's correct.
Apart from the syntax errors you can't put into inside a dynamic string, it needs to be like this:
execute immediate '<query>' into variable;
Re: Procedure., [message #608478 is a reply to message #608467] Thu, 20 February 2014 04:25 Go to previous messageGo to next message
Michel Cadot
Messages: 68643
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

And what happens if there is no "EMP_ID" column in the table in table_name variable?

Re: Procedure., [message #608482 is a reply to message #608468] Thu, 20 February 2014 04:30 Go to previous messageGo to next message
Rarawshenkiv
Messages: 5
Registered: February 2014
Location: Chennai
Junior Member
Sorry, Dude., Since I'm a beginner, I do know about the dynamic sql. Can U kindly explain me the thing which I've done wrong using the query, which I've used???
Re: Procedure., [message #608484 is a reply to message #608468] Thu, 20 February 2014 04:33 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
You need to learn to debug these things yourself:

cookiemonster wrote on Thu, 20 February 2014 09:42

When using dynamic sql you should always display the dynamic string you've built up to check it's correct.


you're already displaying thing with dbms_output, just assign the dynamic query to a variable and display that as well.
Re: Procedure., [message #608497 is a reply to message #608478] Thu, 20 February 2014 05:56 Go to previous message
Michel Cadot
Messages: 68643
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

A space is missing after "select".
And
Quote:
what happens if there is no "EMP_ID" column in the table in table_name variable?

Previous Topic: Terrifying result of sql-query, need explanation.
Next Topic: no of counts from case when
Goto Forum:
  


Current Time: Tue Apr 23 02:33:51 CDT 2024