在 DB2 中创建存储过程时出现语法错误

Syntax error when creating stored procedure in DB2

我正在尝试在 DB2 中创建一个使用游标的存储过程。但是,我遇到了正确语法的问题并收到错误:

    SQL0104N  An unexpected token "=" was found following "N FOR SET v_party_id".  
Expected tokens may include:  "JOIN".  LINE NUMBER=12.  SQLSTATE=42601

我的程序如下:

CREATE PROCEDURE Core.LWRH_LIST_CARRIER_EMAILS
(
    IN p_party_role_id BIGINT
)
LANGUAGE SQL
DYNAMIC RESULT SETS 1
BEGIN


DECLARE v_party_id BIGINT;
DECLARE c_result CURSOR WITH RETURN FOR
SET v_party_id = (Select party_id from core.party_role where party_role_id = p_party_role_id);

Select cm.contact_method_id, cm.contact_method_type_id, cm.electronic_address
from core.party_contact_method pcm
join core.contact_method cm
on cm.contact_method_id = pcm.contact_method_id
and cm.contact_method_type_id = 6
and pcm.party_id = v_party_id;



OPEN c_result;

END@

谁能告诉我正确的语法是什么?

谢谢

至少有一个问题是您没有在 set 语句上定义游标。您在 select 语句中定义它。所以,这条语句:

DECLARE c_result CURSOR WITH RETURN FOR
SET v_party_id = (Select party_id from core.party_role where party_role_id = p_party_role_id);

没有意义。

试试这个:

DECLARE c_result CURSOR WITH RETURN FOR
    Select party_id
    from core.party_role
    where party_role_id = p_party_role_id;

或者,如果只有一行,则省略光标。

您的 SET 语句位于 DECLARE CURSOR 语句的中间。它应该看起来像:

...

DECLARE v_party_id BIGINT;

-- This doesn't execute the statement, just declares the cursor.
DECLARE c_result CURSOR WITH RETURN FOR
    select cm.contact_method_id, cm.contact_method_type_id, cm.electronic_address
    from   core.party_contact_method pcm
      join core.contact_method cm
        on cm.contact_method_id = pcm.contact_method_id
       and cm.contact_method_type_id = 6
       and pcm.party_id = v_party_id;


SET v_party_id = (Select party_id from core.party_role where party_role_id = p_party_role_id);


OPEN c_result;

...