Solved

Truncate tables in Other Schema Oracle Stored Procedure

Posted on 2011-03-22
5
1,267 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
  • 3
  • 2
5 Comments
 
LVL 73

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 73

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 76

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 73

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 76

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
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 how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

762 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now