Informix SQL执行FOREACH游标内的函数

时间:2018-04-30 22:38:31

标签: sql function stored-procedures cursor informix

Informix版本11.70 - 12.10

尝试创建存储过程以获取已锁定表的用户的会话ID,然后使用该会话ID执行内置函数。我没有在Informix中编写太多的程序。

围绕WITH RESUME获取语法错误。

任何帮助?

CREATE PROCEDURE sp_kill_lock ()

RETURNING
  INT as User_Session
;

-- declaration of variables
DEFINE rsUser_Session INT;

-- SELECT each User Session that has a lock on a table
SELECT DISTINCT s.sid AS User_Session
FROM sysmaster:syslocks l, sysmaster:syssessions s 
WHERE s.sid = l.owner 
   AND dbsname <> 'sysmaster' 
ORDER BY 1 
INTO TEMP tempSession;

--Cursor
FOREACH 
   SELECT User_Session
   INTO rsUser_Session
   FROM tempSession

RETURN
rsUser_Session;

--kill the session returned
EXECUTE FUNCTION sysadmin:task("onmode","z",rsUser_Session)

WITH RESUME
;

END FOREACH
END PROCEDURE;


 -- Permissions for routine "sp_kill_lock"
 grant execute on function sp_kill_lock to 'public';

我更改了代码,至少我不再遇到语法错误了。现在它说该函数返回了太多的值:

CREATE PROCEDURE sp_kill_lock ()

RETURNING
  INT as User_Session
;

-- declaration of variables
DEFINE rsUser_Session INT;

-- SELECT each User Session that has a lock on a table
SELECT DISTINCT s.sid AS User_Session
FROM sysmaster:syslocks l, sysmaster:syssessions s 
WHERE s.sid = l.owner 
   AND dbsname <> 'sysmaster' 
ORDER BY 1 
INTO TEMP tempSession;

--Cursor
FOREACH 
   SELECT User_Session
   INTO rsUser_Session
   FROM tempSession

--removed this RETURN
--RETURN
--rsUser_Session;

--kill the session returned
EXECUTE FUNCTION sysadmin:admin("onmode","z",rsUser_Session);

--removed the WITH RESUME

END FOREACH
END PROCEDURE;

测试锁定表:

BEGIN WORK;
LOCK TABLE mytable IN EXCLUSIVE MODE

1 个答案:

答案 0 :(得分:2)

! Missing $ inserted. <inserted text> $ l.380 ... Other Content Finding Features_10_0.png} ? ! Emergency stop. <inserted text> $ l.380 ... Other Content Finding Features_10_0.png} WITH RESUME语句的一部分,因此在您的第一个示例中应该是:

RETURN

但执行时仍会出现错误,因为RETURN rsUser_Session WITH RESUME; 无法返回值。您需要将其更改为PROCEDURE

然后它可能不会执行您想要的操作,因为它在执行CREATE FUNCTION之前返回,因此,例如,如果只有一个会话要终止,它将永远不会执行{{1} }。而且它会留下温度表。

所以让我们重写它,总是执行onmode -z而不使用临时表(我使用的是Informix 12.10.FC10DE):

onmode -z
  1. Informix SPL statements
  2. Informix Relationship Between Routines, Functions, and Procedures