0

我,讓我通過識別用戶名每個用戶的訪問查詢創建過程如何從選擇查詢

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'))); 

,我想使程序此查詢並保存該結果在一排和ESQL

調用這個過程中,如IBM Message Broker的軟件,我用這個程序,但不適合結果:

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; 

請你幫幫我!

回答

0
  1. 您可以將您的查詢重寫爲內部連接而不是子選擇。

    選擇a.actionid,a.actionname,a.allowwrite,從thp.tbaction a.allowread 一個, thp.tbaction_profile AP, thp.tbprofile P, thp.tbuserü 其中a.actionid = ap.actionid 和ap.profid = p.profid 和p.profid = u.profid 和u.userid = P_USERID

  2. 你並不需要一個select語句後提交。

  3. 你有幾個方法可以從你的程序返回的結果:

參考光標

,如果你的應用程序知道如何處理遊標這隻會工作。 JDBC \ ODBC一樣。

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; 

臨時表

插入結果到全局臨時表。當應用程序需要的結果 - 它觸發功能,然後使用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; 
查詢結果臨時表