Solved

Truncate tables in Other Schema Oracle Stored Procedure

Posted on 2011-03-22
5
1,393 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 500 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 500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines

734 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