?
Solved

Truncate tables in Other Schema Oracle Stored Procedure

Posted on 2011-03-22
5
Medium Priority
?
1,525 Views
Last Modified: 2012-08-13
I have a stored procedure and would like to truncate another table from different schema from the current schema (of stored proc).

How do I do this?
0
Comment
Question by:codemonkey2480
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 74

Accepted Solution

by:
sdstuber earned 2000 total points
ID: 35192148
the procedure must be owned by the same schema that owns the table

then you can simply do something like this...

create or replace trunc_table(p_tablename in varchar2)
is
begin
execute immediate 'truncate table ' || p_tablename;
end;

then grant execute on the procedure to your other schema
0
 
LVL 74

Assisted Solution

by:sdstuber
sdstuber earned 2000 total points
ID: 35192166
to add protection against sql injection it would be good to check the parameter to confirm it is the name of a table.  You could wrap it in double quotes within the execute immediate call too.


execute immediate 'truncate table "' || p_tablename || '"';
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 35192219
>>the procedure must be owned by the same schema that owns the table

If you already have been given explicit permission to truncate the other schema's table,  just add the schema to the truncate command:

execute immediate 'truncate table schema.table';
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 35192334
>>> If you already have been given explicit permission to truncate the other schema's table

there is no "truncate table" permission that can be granted

the closest you can get is "drop any table"  which is NOT recommended


also,  small mistake above,  I left out the key word "PROCEDURE"


create or replace procedure trunc_table(p_tablename in varchar2)
is
begin
execute immediate 'truncate table ' || p_tablename;
end;

0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 35192348
Wasn't implying a 'truncate table' permission.  Just stating that if they already had permission to issue a truncate command...
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video shows how to recover a database from a user managed backup
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question