If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Oracle > Insert stored procedure by passing table name as a parameter

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-02-12, 06:41
kannama kannama is offline
Registered User
 
Join Date: Feb 2012
Posts: 9
Insert stored procedure by passing table name as a parameter

Hello
I have this query
but it is not work properly
pls check this and find a solution to this

create or replace procedure bank_insert_sp
(
p_tablename in varchar2,
p_bankcode in varchar2,
p_bankname in varchar2,
p_dist_code in number
)
as
v_tem varchar2(5000);
begin
v_tem:='insert into ' || UPPER(p_tablename) || '
(
bankcode,
bankname,
dist_code
)
values
(
' ||p_bankcode || ',
' ||p_bankname || ',
' ||p_dist_code || '
)';
execute immediate v_tem ;
end bank_insert_sp;
Reply With Quote
  #2 (permalink)  
Old 02-02-12, 08:43
beilstwh beilstwh is offline
Lead Application Develope
 
Join Date: Jun 2004
Location: Liverpool, NY USA
Posts: 2,222
what is the error it is generating? how do you know it's not working? did you commit after the insert? You need to use quotes around strings in the insert. ore information
__________________
Bill
You do not need a parachute to skydive. You only need a parachute to skydive twice.
Reply With Quote
  #3 (permalink)  
Old 02-02-12, 08:52
flyboy flyboy is offline
Registered User
 
Join Date: Mar 2007
Posts: 546
The standard way for debugging dynamic SQL is: construct the statement (V_TEM variable in your case) and then print it - you may use e.g. DBMS_OUTPUT. Copy/paste it verbatim and try to run it statically. Examine the error. Examine the statement and fix the failure cause.

After you will be able to spot the error, just stop hardcoding variable values into V_TEM, but bind them instead:
http://www.akadia.com/services/ora_bind_variables.html
Reply With Quote
  #4 (permalink)  
Old 02-02-12, 14:43
beilstwh beilstwh is offline
Lead Application Develope
 
Join Date: Jun 2004
Location: Liverpool, NY USA
Posts: 2,222
try

Code:
create or replace procedure bank_insert_sp
  (
 p_tablename in  varchar2,
 p_bankcode in varchar2,
 p_bankname in varchar2,
p_dist_code in number
)
  as
  v_tem  varchar2(5000);
 begin
v_tem:='insert into ' || UPPER(p_tablename) || ' 
(
bankcode,
bankname,
dist_code
)
values
(
''' ||p_bankcode || ''',
''' ||p_bankname || ''',
' ||p_dist_code || '
)';
execute  immediate v_tem ;
end  bank_insert_sp;
__________________
Bill
You do not need a parachute to skydive. You only need a parachute to skydive twice.
Reply With Quote
  #5 (permalink)  
Old 02-03-12, 02:09
flyboy flyboy is offline
Registered User
 
Join Date: Mar 2007
Posts: 546
Quote:
Originally Posted by beilstwh View Post
try
What about being kind to your shared pool and CPU and not to propose non-scalable solution? There is more effective way described (with examples) in the link I posted...

Also, be prepared for another tweaking of your code, just after the bank name will contain single quote (').
Reply With Quote
  #6 (permalink)  
Old 02-03-12, 04:30
kannama kannama is offline
Registered User
 
Join Date: Feb 2012
Posts: 9
Reply

Thank you for your kind information regarding the stored procedure for the insert query
I have more queries to clarify hope u will help me
I paste the Query n below


create or replace procedure bank_search_sp
(
p_tablename in varchar2,
p_searchname in varchar2,
p_bankcode out varchar2,
p_bankname out varchar2,
p_dist_code out number
)
as
v_tem varchar2(5000);
begin
v_tem :='select bankcode,bankname,dist_code from ' || UPPER (p_tablename) || '
where bankname like '''|| p_searchname||'';
execute immediate v_tem into p_bankcode,p_bankname,p_dist_code using p_searchname ;
end bank_search_sp;



This is a stored procedure for searching data from table .Here the procedure is created but it will not work properly.I hope u will consider this also
Reply With Quote
  #7 (permalink)  
Old 02-03-12, 05:02
flyboy flyboy is offline
Registered User
 
Join Date: Mar 2007
Posts: 546
Quote:
Originally Posted by kannama View Post
I paste the Query n below
I doubt it - the count of single quotes around P_SEARCHNAME is wrong. Have you not considered to bind that variable(s) instead? Why? You already use INTO clause in EXECUTE IMMEDIATE.
Quote:
Originally Posted by kannama View Post
Here the procedure is created but it will not work properly.I hope u will consider this also
Alas! yet another mysterious "not work properly" Oracle behaviour. You are very unlucky to fall into such a dreadful situation; in fact I never faced it, Oracle did not document it anywhere, so I cannot assist more.
Besides, without defining, what "work properly" means, especially when multiple rows satisfy that condition, it is impossible to suggest anything.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On