2

If I run this in Oracle SQL developer:

    set serveroutput on

DECLARE 
    key_column varchar2(30) := 'Test';
BEGIN
    dbms_output.put_line('key_col = ' || key_column );

    SELECT cols.column_name INTO key_column
    FROM all_constraints cons
    INNER JOIN all_cons_columns cols ON cons.constraint_name = cols.constraint_name
                                    AND cons.owner = cols.owner
                                    AND cons.constraint_type = 'P'
    WHERE cons.owner = 'TAS'
    AND cols.table_name = 'COMMENT_ALL_TXT';

   DBMS_OUTPUT.PUT_LINE('1 key_column: ' || key_column);

END 
; 

The output is:

anonymous block completed

key_col = Test

1 key_column: COM_ALL_TXT_KEY

When I put the same code into a procedure, this is the output:

ORA-01403: no data found

Why does the procedure code not find the constraint when the code run in a SQL Worksheet does?

Here is the procedure code:

create or replace PROCEDURE MMW1164.USP_BATCH_CLEANUP_NEW
IS
key_column VARCHAR2(100);
BEGIN

  SELECT cols.column_name INTO key_column
    FROM all_constraints cons
    INNER JOIN all_cons_columns cols ON cons.constraint_name = cols.constraint_name
                                    AND cons.owner = cols.owner
                                    AND cons.constraint_type = 'P'
    WHERE cons.owner = 'TAS'
    AND cols.table_name = 'COMMENT_ALL_TXT';

  DBMS_OUTPUT.PUT_LINE('1 key_column: ' || key_column);

END 
; 
Matthew Walk
  • 1,014
  • 2
  • 16
  • 36
  • If there is something wrong with my question, please comment to let me know. Handing out a -1 with no comment doesn't help. – Matthew Walk Mar 27 '17 at 16:43
  • Not the dv, but please show the procedure code. Are you running the direct query and stored proc in the same schema? – OldProgrammer Mar 27 '17 at 16:54
  • Thanks for the recommendation OldProgrammer. I am running the direct query and the stored proc in the same schema. – Matthew Walk Mar 27 '17 at 17:02
  • Is the schema you are running the stored proc out different than "TAS"? – OldProgrammer Mar 27 '17 at 17:04
  • Yes, the schema is "MMW1164". – Matthew Walk Mar 27 '17 at 17:05
  • Are MMW1164's permissions against the TAS schema objects granted directly, or via a role? From the behaviour is looks like via a role, and you're seeing an [invoker's rights](https://docs.oracle.com/database/121/LNPLS/subprograms.htm#LNPLS00809) issue (see authid in the docs). But please confirm... – Alex Poole Mar 27 '17 at 17:28
  • 1
    [Possible duplicate](http://stackoverflow.com/q/4399528/266304) if permissions *are* coming from a role. – Alex Poole Mar 27 '17 at 17:31
  • What does `select grantee from all_tab_privs where grantor = 'TAS' and table_name = 'COMMENT_ALL_TXT'`? Does it show your name, or a role you can also see in `user_role_privs`? – Alex Poole Mar 27 '17 at 17:41

1 Answers1

1

If you are running the procedure from the same schema, it should not give error.
Please check the attached screen shot. I just replicated your scenario.
Please cross check your code with mine on. Thanks. enter image description here

Make sure, your schema name is right. I mean, you are checking both under the same schema. Then result will be same.

smshafiqulislam
  • 564
  • 6
  • 12
  • Thanks for the answer smshafiqulislam. When I run the direct query, I'm running it connected to the DB as "MMW1164" and it works when filtering for the schema "TAS". When I create the procedure, it's schema is "MMW1164" and it's attempting to query the schema "TAS" but apparently coming back with no value. – Matthew Walk Mar 27 '17 at 17:22
  • What is the output of this query? SELECT cols.column_name FROM all_constraints cons INNER JOIN all_cons_columns cols ON cons.constraint_name = cols.constraint_name AND cons.owner = cols.owner AND cons.constraint_type = 'P' WHERE cons.owner = 'TAS' AND cols.table_name = 'COMMENT_ALL_TXT'; – smshafiqulislam Mar 27 '17 at 17:25
  • I shared the screen shot for your reference. It should work. :) Just share what is the output of query under "MMW1164" – smshafiqulislam Mar 27 '17 at 17:29
  • Hi again smshafiqulislam. When I run that query in Oracle SQL developer, it returns "COM_ALL_TXT_KEY". When that same query runs in the procedure and tries to save the value into the variable, the variable is empty. It must be something to do with the fact that the procedure is in the schema "MMW1164" but the query is trying to filter for owner = 'TAS'. I just don't understand how it works for a direct query, but not in the procedure. – Matthew Walk Mar 27 '17 at 17:33
  • Oh, I see. I got your problem. You need dba privilege if you want to see other schema's constraint information sitting in one schema. MMW1164 needs dba privilege. Check by just running "grant dba to MMW1164" once. – smshafiqulislam Mar 27 '17 at 17:39
  • @smshafiqulislam - that's massive overkill and dangerous, and goes completely against the principle of least privilege. The table privileges just need to be granted directly, or the procedure needs to be created with `authid current_user`. – Alex Poole Mar 27 '17 at 17:43
  • @AlexPoole yes. I agree. I mentioned just to make the things clear that, it is privilege issue. Yes. Solution is granting table privileges or using authid. – smshafiqulislam Mar 27 '17 at 17:48
  • After asking the DBAs to create another version of the proc in the "TAS" schema, it worked. So, it does appear to be a schema permissions related issue. – Matthew Walk Mar 28 '17 at 15:57