Pls Ql Advance

8
AUTHID DEFINER V/S AUTHID CURRENT_USER There are lot of times we get error in Oracle Apps while trying to execute the API's at development time, due to the AUTHID DEFINER and AUTHID CURRENT_USER. This article gives you good understanding about the AUTHID DEFINER and AUTHID CURRENT_USER. A stored procedure runs either with the rights of the caller (AUTHID CURRENT_USER) or with the rights of the procedure's owner (AUTHID DEFINER). This behaviour is specified with the AUTHID clause. This authid clause immediatly follows the create procedure, create function, create package or create type statement. It can be ommited, in which case the default authid definer is taken. AUTHID DEFINER and AUTHID CURRENT_USER ----------------------------------------------------------- AUTHID DEFINER:- -------------------- Example:- --------- The following are done in APPS scheme. create table a (a_a number); CREATE OR REPLACE PACKAGE XYZ AUTHID DEFINER AS PROCEDURE XYZ_1; END XYZ; CREATE OR REPLACE PACKAGE body XYZ AS PROCEDURE XYZ_1 IS BEGIN INSERT INTO A VALUES (1); END XYZ_1; END XYZ;

description

Pls Ql Advance

Transcript of Pls Ql Advance

Page 1: Pls Ql Advance

AUTHID DEFINER V/S AUTHID CURRENT_USER

There are lot of times we get error in Oracle Apps while trying to execute the API's at development time, due to the AUTHID DEFINER and AUTHID CURRENT_USER. This article gives you good understanding about the AUTHID DEFINER and AUTHID CURRENT_USER.

A stored procedure runs either with the rights of the caller (AUTHID CURRENT_USER) or with the rights of the procedure's owner (AUTHID DEFINER). This behaviour is specified with the AUTHID clause. This authid clause immediatly follows the create procedure, create function, create package or create type statement. It can be ommited, in which case the default authid definer is taken.

AUTHID DEFINER and AUTHID CURRENT_USER-----------------------------------------------------------AUTHID DEFINER:---------------------

Example:----------The following are done in APPS scheme.

create table a (a_a number);

CREATE OR REPLACE PACKAGE XYZ AUTHID DEFINERASPROCEDURE XYZ_1;END XYZ;

CREATE OR REPLACE PACKAGE body XYZASPROCEDURE XYZ_1ISBEGININSERT INTO A VALUES (1);END XYZ_1;END XYZ;

beginxyz.XYZ_1;end;

select * from a;

Provide grants for this package to other schema (scott) and create the synonym for the xyz package in scott.

Page 2: Pls Ql Advance

grant all on to

Example:----------

grant all on xyz to scott

Above command is run from the apps schema.

Now for the other schema SCOTT try to run the same query.

beginxyz.XYZ_1;end;

It have inserted new record in the 'A' table. Note there is no synonym for the table A in SCOTT schema.

Running this program from anywhere, it is as good as running from APPS schema in case of AUTHID DEFINER.

10.2) CURRENT_USER:-----------------------

Example:------------

The following are done in the APPS schema.

create table a (a_a number);

CREATE OR REPLACE PACKAGE XYZ AUTHID CURRENT_USERASPROCEDURE XYZ_1;END XYZ;

CREATE OR REPLACE PACKAGE body XYZASPROCEDURE XYZ_1ISBEGININSERT INTO A VALUES (1);END XYZ_1;END XYZ;

Page 3: Pls Ql Advance

beginxyz.XYZ_1;end;

select * from a;

Provide grants for this package to other schema (scott) and create the synonym for the xyz package in scott.

grant all on to

Example:------------

grant all on xyz to scott

Above command is run from the apps schema.

Now for the other schema (scott) try to run the same query.

beginxyz.XYZ_1;end;

Got the error message table or view doesn't exist for the A table.

Create view for the a table and run the same program again.

create synonym 'A' for table 'A'

beginxyz.XYZ_1;end;

select * from a;

Now there is no error. It is inserting the record with no issue.

WITH NO AUTHID DEFINER and AUTHID CURRENT_USER :------------------------------------------------------------------------

Example:----------

The following are done in the APPS schema.

Page 4: Pls Ql Advance

create table a (a_a number);

CREATE OR REPLACE PACKAGE XYZASPROCEDURE XYZ_1;END XYZ;

CREATE OR REPLACE PACKAGE body XYZASPROCEDURE XYZ_1ISBEGININSERT INTO A VALUES (1);END XYZ_1;END XYZ;

beginxyz.XYZ_1;end;

select * from a;

Provide grants for this package to other schema (scott) and create the synonym for the xyz package in scott.

grant all on to

Example:----------

grant all on xyz to scott

Above command is run from the apps schema.

Now for the other schema SCOTT try to run the same query.

beginxyz.XYZ_1;end;

It is working in same way as it have done for the AUTHID DEFINER.

Q) Is it possible to know from the select statement if it is INVOKER(CURRENT_USER) or DEFINER

Page 5: Pls Ql Advance

A) Yes, It is possible to get this information from the select statement. Use

SELECT dbo.object_name,(DECODE(SIGN(bitand(options,16)),1,'INVOKER','DEFINER')) "authid"FROM dba_objects dbo,sys.PROCEDURE$ pWHERE p.obj# = dbo.object_idAND dbo.object_name = "Your Package Name"AND dbo.object_type = 'PACKAGE'AND dbo.owner = 'APPS'

Example:------------

SELECT dbo.object_name,(DECODE(SIGN(bitand(options,16)),1,'INVOKER','DEFINER')) "authid"FROM dba_objects dbo,sys.PROCEDURE$ pWHERE p.obj# = dbo.object_idAND dbo.object_name = 'ASO_APR_WF_INT'AND dbo.object_type = 'PACKAGE'AND dbo.owner = 'APPS'

How to Select the Nth highest / lowest value from a table:-

I am posting this script as lot of my friends have asked about this. They are lot of ways to do this. I am showing you in two best ways.

Note:- In 'N' place, provide your Nth number you want to findout.

How to Select the Nth highest value from a table:--------------------------------------------------

select level, max(sal) from scott.empwhere level = '&n'connect by prior (sal) > salgroup by level;

How to select the Nth lowest value from a table:--------------------------------------------------------select level, min(sal) from scott.empwhere level = '&n'connect by prior (sal) <>N th Top Salary-------------------

Page 6: Pls Ql Advance

select a.ename,a.salfrom emp awhere n = (select count(distinct(b.sal))from emp b where a.sal <= b.sal) N th Least Salary---------------------select a.ename,a.salfrom emp awhere n = (select count(distinct(b.sal))from emp bwhere a.sal >= b.sal)

Duplicate rows in the table

Duplicate rows in the table:---------------------------------

The following query can be used to get the duplicate records from table.

SELECT * FROM 'Your table name' WHERE ROWID NOT IN (SELECT MAX(ROWID) FROM 'Your Table Name' GROUP BY 'Your duplicate values field name');

Example:-

SELECT * FROM emp WHERE ROWID NOT IN (SELECT MAX(ROWID) FROM emp GROUP BY ename);

To eliminate/delete the duplicate rows from the table, you can use the following query.

DELETE 'Your table name' WHERE ROWID NOT IN (SELECT MAX(ROWID) FROM 'Your Table Name' GROUP BY 'Your duplicate values field name');

Example:-

DELETE emp WHERE ROWID NOT IN (SELECT MAX(ROWID) FROM emp GROUP BYename);