Solved

Truncate tables in Other Schema Oracle Stored Procedure

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

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

Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

821 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