Domanda

I have a query that give me access of each user by identified UserID:

My query is :

SELECT A.ACTIONID,A.ACTIONNAME,A.ALLOWWRITE,A.ALLOWREAD
                                        FROM  THP.TBACTION  A   
                                        WHERE A.ACTIONID   IN ( SELECT AP.ACTIONID
                                        FROM   THP.TBACTION_PROFILE  AP
                                        WHERE  AP.PROFID IN(SELECT P.PROFID
                                        FROM THP.TBPROFILE  P
                                        WHERE P.PROFID IN(SELECT U.PROFID
                                                     FROM  THP.TBUSER  U
                                                     WHERE U.USERID='1' )));

and I want to make procedure for this query and save result of that in a ROW and call this procedure in software like IBM Message Broker in ESQL

I use this procedure but not suitable result :

create or replace
PROCEDURE     SELECT_ACTION (
     P_USERID    IN       NUMBER,

    RESULT        OUT      NUMBER)IS
 CNT NUMBER;
BEGIN
     RESULT := 1;
     CNT    := 0;
     SELECT COUNT(1) INTO CNT FROM THP.TBUSER WHERE USERID = P_USERID ;--AND SERIALTOKEN= P_SERIALTOKEN;

     IF CNT = 1 THEN  
        BEGIN  
            SELECT A.ACTIONID,A.ACTIONNAME,A.ALLOWWRITE,A.ALLOWREAD
                                        FROM  THP.TBACTION  A   
                                        WHERE A.ACTIONID   IN ( SELECT AP.ACTIONID
                                        FROM   THP.TBACTION_PROFILE  AP
                                        WHERE  AP.PROFID IN(SELECT P.PROFID
                                        FROM THP.TBPROFILE  P
                                        WHERE P.PROFID IN(SELECT U.PROFID
                                                     FROM  THP.TBUSER  U
                                                     WHERE U.USERID=P_USERID )));
         COMMIT;
         RESULT :=0;  -- ROW was Found     
        END;  

     END IF;
   EXCEPTION
     WHEN NO_DATA_FOUND THEN
       RESULT := 3;
     WHEN OTHERS THEN
       RESULT := 4;
END SELECT_ACTION;

would you please help me!!!

È stato utile?

Soluzione

  1. You can rewrite your query to inner joins instead of sub selects.

    select a.actionid,a.actionname,a.allowwrite,a.allowread from thp.tbaction a , thp.tbaction_profile ap , thp.tbprofile p, thp.tbuser u where a.actionid = ap.actionid and ap.profid = p.profid and p.profid = u.profid and u.userid=P_USERID

  2. You don't need to commit after a select statement.

  3. You have a couple of ways to return results from your procedure:

Ref Cursor

this will work only if your application knows how to handle cursors. jdbc\odbc does.

create or replace function select_action (
     p_userid    in       number)
is
    result sys_refcursor;
begin 
    open result for 
        select  a.actionid,a.actionname,a.allowwrite,a.allowread
        from    thp.tbaction  a  , 
                thp.tbaction_profile ap , 
                thp.tbprofile  p, 
                thp.tbuser  u
        where   a.actionid = ap.actionid
        and     ap.profid = p.profid
        and     p.profid = u.profid
        and     u.userid=P_USERID;

    if not result%found then 
        raise_application_error(-20001 , 'user ' || p_userid ||' not found')
    end if;

    return result;
end;

Temporary table

you insert the result into a global temp table. when the application wants result - it activates the function and then query the result temp table using the user_id

create global temporary table temp_select_action_result 
as
    select  a.actionid,a.actionname,a.allowwrite,a.allowread
    from    thp.tbaction  a 
    where   1 = 0

procedure select_action (
    p_userid    in       number)
is
begin 
    insert into temp_select_action_result
        select  a.actionid,a.actionname,a.allowwrite,a.allowread
        from    thp.tbaction  a  , 
                thp.tbaction_profile ap , 
                thp.tbprofile  p, 
                thp.tbuser  u
        where   a.actionid = ap.actionid
        and     ap.profid = p.profid
        and     p.profid = u.profid
        and     u.userid=P_USERID;
end;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top