1

我正在尝试设置输出参数 thirdPartyId 的值,但missing or invalid optionset thirdPartyId声明中出现错误提示。

PROCEDURE usp_insert_user(  userType VARCHAR2,
                                logonId VARCHAR2,
                                title VARCHAR2,
                                firstName VARCHAR2,
                                middleName VARCHAR2,
                                lastName VARCHAR2,
                                comments VARCHAR2,
                                thirdPartyId OUT number) AS
      begin
        set thirdPartyId := select max(third_party_id) + 1 from third_party_user_temp;
        insert into THIRD_PARTY_USER_TEMP
            (Third_party_id,web_logon_id,prsn_type_cd,prsn_title_nm,
            prsn_first_nm,prsn_mdl_nm,prsn_last_nm,addtnl_third_party_txt)
            VALUES(thirdPartyId,logonId,upper(userType),title,
            firstName,middleName,lastName,comments)
        ;
    end usp_insert_user;

这样做的正确方法是什么?

谢谢!

更新:这更安全吗?

insert into THIRD_PARTY_USER_TEMP
        (Third_party_id,web_logon_id,prsn_type_cd,prsn_title_nm,
        prsn_first_nm,prsn_mdl_nm,prsn_last_nm,addtnl_third_party_txt)
        VALUES((select max(third_party_id) + 1 from third_party_user_temp),logonId,upper(userType),title,
        firstName,middleName,lastName,comments)
        returning third_party_id into thirdPartyId
4

1 回答 1

3

你可以这样做:

select max(third_party_id) + 1 into thirdPartyId from third_party_user_temp;

如果两个人可以同时运行它,这可能会出现争用问题 - 两者都可能以相同的 new 结束thirdPartyId。您可以查看序列来避免这种情况。


如果您定义一个名为 的序列thirdPartyIdSeq,您可以这样做:

PROCEDURE usp_insert_user(  userType VARCHAR2,
                                logonId VARCHAR2,
                                title VARCHAR2,
                                firstName VARCHAR2,
                                middleName VARCHAR2,
                                lastName VARCHAR2,
                                comments VARCHAR2,
                                thirdPartyId OUT number) AS
      begin
        insert into THIRD_PARTY_USER_TEMP
            (Third_party_id,web_logon_id,prsn_type_cd,prsn_title_nm,
            prsn_first_nm,prsn_mdl_nm,prsn_last_nm,addtnl_third_party_txt)
            VALUES(third_party_id_seq.nextval,logonId,upper(userType),title,
            firstName,middleName,lastName,comments)
        returning third_party_id into thirdPartyId;
    end usp_insert_user;

这使用序列生成下一个 ID,并且该returning子句填充您的 OUT 参数。

您还可以避免该过程并在触发器中生成 ID,再次使用该序列。有很多这种方法的例子,尤其是在这个网站上。

CREATE OR REPLACE TRIGGER third_party_user_temp_bi
BEFORE INSERT ON third_party_user_temp
FOR EACH ROW
BEGIN
    SELECT thirdPartyIdSeq.NEXTVAL
    INTO   :new.third_party_id
    FROM   dual;
END;
/

然后,您的插入不需要指定要使用的 ID。

于 2011-03-01T12:16:00.807 回答