Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 从Oracle存储过程返回输出参数_Sql_Oracle_Stored Procedures_Output Parameter - Fatal编程技术网

Sql 从Oracle存储过程返回输出参数

Sql 从Oracle存储过程返回输出参数,sql,oracle,stored-procedures,output-parameter,Sql,Oracle,Stored Procedures,Output Parameter,我试图设置输出参数thirdPartyId的值,但在set thirdPartyId语句中出现错误,显示missing或invalid option PROCEDURE usp_insert_user( userType VARCHAR2, logonId VARCHAR2, title VARCHAR2,

我试图设置输出参数thirdPartyId的值,但在
set thirdPartyId
语句中出现错误,显示
missing或invalid option

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
您可以这样做:

select max(third_party_id) + 1 into thirdPartyId from third_party_user_temp;
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;
如果两个人可以同时运行此程序,则可能会出现争用问题-两人最终可能会使用相同的新
thirdPartyId
。您可以查看序列以避免这种情况。
如果定义了一个名为thirdPartyIdSeq的序列,例如,
thirdPartyIdSeq
,则可以执行以下操作:

select max(third_party_id) + 1 into thirdPartyId from third_party_user_temp;
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;
/

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

是否有一种方法可以在存储过程开始时锁定表,并在结束时将其解锁以避免出现这种情况?我认为,即使我对id使用某种形式的标识,我也必须返回新的id,我可能会遇到相同的问题,如果两个人插入一条记录,然后它返回两个人的id,他们将获得相同的id…显式锁定可能不是您想要深入研究的问题。序列就是这样。序列是神谕版的身份?据我所知,你所说的身份是什么意思,有点像。Oracle没有内置列值的自动递增ID。序列是一个递增的计数器,不会向两个会话返回相同的值,因此它可以用来生成等同于自动递增的ID,但需要一些代码(通常在触发器中)才能将下一个值分配给列。Oracle让我很难过:(.无论如何,我正在更新我的问题。新方法更安全吗?