我有一个查询,可以通过已识别的UserID访问每个用户:
我的查询是:
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' )));
我希望为此查询创建过程并将其结果保存在ROW中,并在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 :(得分:0)
您可以将查询重写为内部联接而不是子选择。
选择a.actionid,a.actionname,a.allowwrite,a.allowread 来自thp.tbaction a, 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
您不需要在选择语句后提交。
您可以通过多种方式从过程中返回结果:
参考光标
只有当您的应用程序知道如何处理游标时,这才有效。 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;