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

时间:2015-03-11 11:51:59

标签: sql stored-procedures cursor 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@

有人可以建议正确的语法是什么吗?

由于

2 个答案:

答案 0 :(得分:2)

您的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;

...

答案 1 :(得分:0)

至少有一个问题是您没有在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;

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