Solved

Truncate tables in Other Schema Oracle Stored Procedure

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
Via a live example, show how to take different types of Oracle backups using RMAN.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

930 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

8 Experts available now in Live!

Get 1:1 Help Now