这个 PL/SQL 脚本有什么问题吗?

发布于 2024-09-16 19:11:59 字数 1828 浏览 4 评论 0原文

VARIABLE StayWorkflow_Id_max number;

BEGIN
  SELECT max(StayWorkflow_Id)+1 into :StayWorkflow_Id_max  from MVStayWorkflow;
END;
/

insert into MVStayWorkflow (StayWorkflow_Id, Stay_Id, Passage_Id,
            User_Id, RespUnit_Id, Resource_Id, WorkflowAction, CurrentState,
            PreviousState, WorkflowTime, UserStamp, TimeStamp)
        values (:StayWorkflow_Id_max, 98485, 129844, 1, '', '', 11, 7, 7, to_char(sysdate, 'YYYYMMDD HH:MI:SS'), 1, to_char(sysdate, 'YYYYMMDD HH:MI:SS'));

insert into MVStayWorkflow (StayWorkflow_Id, Stay_Id, Passage_Id,
            User_Id, RespUnit_Id, Resource_Id, WorkflowAction, CurrentState,
            PreviousState, WorkflowTime, UserStamp, TimeStamp)
        values (:StayWorkflow_Id_max+1, 66311, 114593, 1, '', '', 11, 7, 7, to_char(sysdate, 'YYYYMMDD HH:MI:SS'), 1, to_char(sysdate, 'YYYYMMDD HH:MI:SS'));

insert into MVStayWorkflow (StayWorkflow_Id, Stay_Id, Passage_Id,
            User_Id, RespUnit_Id, Resource_Id, WorkflowAction, CurrentState,
            PreviousState, WorkflowTime, UserStamp, TimeStamp)
        values (:StayWorkflow_Id_max+2, 83742, 110157, 1, '', '', 11, 7, 7, to_char(sysdate, 'YYYYMMDD HH:MI:SS'), 1, to_char(sysdate, 'YYYYMMDD HH:MI:SS'));

insert into MVStayWorkflow (StayWorkflow_Id, Stay_Id, Passage_Id,
            User_Id, RespUnit_Id, Resource_Id, WorkflowAction, CurrentState,
            PreviousState, WorkflowTime, UserStamp, TimeStamp)
        values (:StayWorkflow_Id_max+3, 74421, 98685, 1, '', '', 11, 7, 7, to_char(sysdate, 'YYYYMMDD HH:MI:SS'), 1, to_char(sysdate, 'YYYYMMDD HH:MI:SS'));

update SYSQLSequence set nextValue = :StayWorkflow_Id_max+4 where name = '_MVStayWorkflow_auto_';

commit;

给我这个错误:

Error report:
SQL Error: Missing IN or OUT parameter at index:: 1
VARIABLE StayWorkflow_Id_max number;

BEGIN
  SELECT max(StayWorkflow_Id)+1 into :StayWorkflow_Id_max  from MVStayWorkflow;
END;
/

insert into MVStayWorkflow (StayWorkflow_Id, Stay_Id, Passage_Id,
            User_Id, RespUnit_Id, Resource_Id, WorkflowAction, CurrentState,
            PreviousState, WorkflowTime, UserStamp, TimeStamp)
        values (:StayWorkflow_Id_max, 98485, 129844, 1, '', '', 11, 7, 7, to_char(sysdate, 'YYYYMMDD HH:MI:SS'), 1, to_char(sysdate, 'YYYYMMDD HH:MI:SS'));

insert into MVStayWorkflow (StayWorkflow_Id, Stay_Id, Passage_Id,
            User_Id, RespUnit_Id, Resource_Id, WorkflowAction, CurrentState,
            PreviousState, WorkflowTime, UserStamp, TimeStamp)
        values (:StayWorkflow_Id_max+1, 66311, 114593, 1, '', '', 11, 7, 7, to_char(sysdate, 'YYYYMMDD HH:MI:SS'), 1, to_char(sysdate, 'YYYYMMDD HH:MI:SS'));

insert into MVStayWorkflow (StayWorkflow_Id, Stay_Id, Passage_Id,
            User_Id, RespUnit_Id, Resource_Id, WorkflowAction, CurrentState,
            PreviousState, WorkflowTime, UserStamp, TimeStamp)
        values (:StayWorkflow_Id_max+2, 83742, 110157, 1, '', '', 11, 7, 7, to_char(sysdate, 'YYYYMMDD HH:MI:SS'), 1, to_char(sysdate, 'YYYYMMDD HH:MI:SS'));

insert into MVStayWorkflow (StayWorkflow_Id, Stay_Id, Passage_Id,
            User_Id, RespUnit_Id, Resource_Id, WorkflowAction, CurrentState,
            PreviousState, WorkflowTime, UserStamp, TimeStamp)
        values (:StayWorkflow_Id_max+3, 74421, 98685, 1, '', '', 11, 7, 7, to_char(sysdate, 'YYYYMMDD HH:MI:SS'), 1, to_char(sysdate, 'YYYYMMDD HH:MI:SS'));

update SYSQLSequence set nextValue = :StayWorkflow_Id_max+4 where name = '_MVStayWorkflow_auto_';

commit;

Gives me this error:

Error report:
SQL Error: Missing IN or OUT parameter at index:: 1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

自由如风 2024-09-23 19:11:59

您的代码并不是真正的 PL/SQL,而主要是 SQL*plus 脚本(特别是 VARIABLE 既不是 PL/SQL 也不是 SQL)。

为什么不把它变成纯 PL/SQL:

DECLARE
  StayWorkflow_Id_max number;

BEGIN
  SELECT max(StayWorkflow_Id)+1 into StayWorkflow_Id_max  from MVStayWorkflow;

  insert into MVStayWorkflow (StayWorkflow_Id, Stay_Id, Passage_Id,
            User_Id, RespUnit_Id, Resource_Id, WorkflowAction, CurrentState,
            PreviousState, WorkflowTime, UserStamp, TimeStamp)
        values (StayWorkflow_Id_max, 98485, 129844, 1, '', '', 11, 7, 7, to_char(sysdate, 'YYYYMMDD HH:MI:SS'), 1, to_char(sysdate, 'YYYYMMDD HH:MI:SS'));

  insert into MVStayWorkflow (StayWorkflow_Id, Stay_Id, Passage_Id,
            User_Id, RespUnit_Id, Resource_Id, WorkflowAction, CurrentState,
            PreviousState, WorkflowTime, UserStamp, TimeStamp)
        values (StayWorkflow_Id_max+1, 66311, 114593, 1, '', '', 11, 7, 7, to_char(sysdate, 'YYYYMMDD HH:MI:SS'), 1, to_char(sysdate, 'YYYYMMDD HH:MI:SS'));

  insert into MVStayWorkflow (StayWorkflow_Id, Stay_Id, Passage_Id,
            User_Id, RespUnit_Id, Resource_Id, WorkflowAction, CurrentState,
            PreviousState, WorkflowTime, UserStamp, TimeStamp)
        values (StayWorkflow_Id_max+2, 83742, 110157, 1, '', '', 11, 7, 7, to_char(sysdate, 'YYYYMMDD HH:MI:SS'), 1, to_char(sysdate, 'YYYYMMDD HH:MI:SS'));

  insert into MVStayWorkflow (StayWorkflow_Id, Stay_Id, Passage_Id,
            User_Id, RespUnit_Id, Resource_Id, WorkflowAction, CurrentState,
            PreviousState, WorkflowTime, UserStamp, TimeStamp)
        values (StayWorkflow_Id_max+3, 74421, 98685, 1, '', '', 11, 7, 7, to_char(sysdate, 'YYYYMMDD HH:MI:SS'), 1, to_char(sysdate, 'YYYYMMDD HH:MI:SS'));

  update SYSQLSequence set nextValue = StayWorkflow_Id_max+4 where name = '_MVStayWorkflow_auto_';

  commit;
END;
/

Your code is not really PL/SQL, but mainly a SQL*plus script (in particular VARIABLE is neither PL/SQL nor SQL).

Why don't you make it pure PL/SQL:

DECLARE
  StayWorkflow_Id_max number;

BEGIN
  SELECT max(StayWorkflow_Id)+1 into StayWorkflow_Id_max  from MVStayWorkflow;

  insert into MVStayWorkflow (StayWorkflow_Id, Stay_Id, Passage_Id,
            User_Id, RespUnit_Id, Resource_Id, WorkflowAction, CurrentState,
            PreviousState, WorkflowTime, UserStamp, TimeStamp)
        values (StayWorkflow_Id_max, 98485, 129844, 1, '', '', 11, 7, 7, to_char(sysdate, 'YYYYMMDD HH:MI:SS'), 1, to_char(sysdate, 'YYYYMMDD HH:MI:SS'));

  insert into MVStayWorkflow (StayWorkflow_Id, Stay_Id, Passage_Id,
            User_Id, RespUnit_Id, Resource_Id, WorkflowAction, CurrentState,
            PreviousState, WorkflowTime, UserStamp, TimeStamp)
        values (StayWorkflow_Id_max+1, 66311, 114593, 1, '', '', 11, 7, 7, to_char(sysdate, 'YYYYMMDD HH:MI:SS'), 1, to_char(sysdate, 'YYYYMMDD HH:MI:SS'));

  insert into MVStayWorkflow (StayWorkflow_Id, Stay_Id, Passage_Id,
            User_Id, RespUnit_Id, Resource_Id, WorkflowAction, CurrentState,
            PreviousState, WorkflowTime, UserStamp, TimeStamp)
        values (StayWorkflow_Id_max+2, 83742, 110157, 1, '', '', 11, 7, 7, to_char(sysdate, 'YYYYMMDD HH:MI:SS'), 1, to_char(sysdate, 'YYYYMMDD HH:MI:SS'));

  insert into MVStayWorkflow (StayWorkflow_Id, Stay_Id, Passage_Id,
            User_Id, RespUnit_Id, Resource_Id, WorkflowAction, CurrentState,
            PreviousState, WorkflowTime, UserStamp, TimeStamp)
        values (StayWorkflow_Id_max+3, 74421, 98685, 1, '', '', 11, 7, 7, to_char(sysdate, 'YYYYMMDD HH:MI:SS'), 1, to_char(sysdate, 'YYYYMMDD HH:MI:SS'));

  update SYSQLSequence set nextValue = StayWorkflow_Id_max+4 where name = '_MVStayWorkflow_auto_';

  commit;
END;
/
可遇━不可求 2024-09-23 19:11:59

正如 wallyk 所指出的,生成 ID 的方式很可疑。通常,它是这样的:

create sequence StayWorkflow_Id nocache;  /* you only do that once */

insert into MVStayWorkflow (StayWorkflow_Id, Stay_Id, Passage_Id,
        User_Id, RespUnit_Id, Resource_Id, WorkflowAction, CurrentState,
        PreviousState, WorkflowTime, UserStamp, TimeStamp)
    values (StayWorkflow_Id.nextval, 98485, 129844, 1, '', '', 11, 7, 7,
            to_char(sysdate, 'YYYYMMDD HH:MI:SS'), 1, to_char(sysdate, 'YYYYMMDD HH:MI:SS'));


insert into MVStayWorkflow (StayWorkflow_Id, Stay_Id, Passage_Id,
        User_Id, RespUnit_Id, Resource_Id, WorkflowAction, CurrentState,
        PreviousState, WorkflowTime, UserStamp, TimeStamp)
    values (StayWorkflow_Id.nextval, 66311, 114593, 1, '', '', 11, 7, 7,
            to_char(sysdate, 'YYYYMMDD HH:MI:SS'), 1, to_char(sysdate, 'YYYYMMDD HH:MI:SS'));


 ...

每次只需使用 StayWorkflow_Id.nextval 获取一个新的唯一 id,而不用担心竞争条件

As wallyk has pointed out, the way you are generating your IDs is fishy. Usually, it goes like this:

create sequence StayWorkflow_Id nocache;  /* you only do that once */

insert into MVStayWorkflow (StayWorkflow_Id, Stay_Id, Passage_Id,
        User_Id, RespUnit_Id, Resource_Id, WorkflowAction, CurrentState,
        PreviousState, WorkflowTime, UserStamp, TimeStamp)
    values (StayWorkflow_Id.nextval, 98485, 129844, 1, '', '', 11, 7, 7,
            to_char(sysdate, 'YYYYMMDD HH:MI:SS'), 1, to_char(sysdate, 'YYYYMMDD HH:MI:SS'));


insert into MVStayWorkflow (StayWorkflow_Id, Stay_Id, Passage_Id,
        User_Id, RespUnit_Id, Resource_Id, WorkflowAction, CurrentState,
        PreviousState, WorkflowTime, UserStamp, TimeStamp)
    values (StayWorkflow_Id.nextval, 66311, 114593, 1, '', '', 11, 7, 7,
            to_char(sysdate, 'YYYYMMDD HH:MI:SS'), 1, to_char(sysdate, 'YYYYMMDD HH:MI:SS'));


 ...

just use StayWorkflow_Id.nextval to get a new unique id each time, WITHOUT BEING AFRAID OF RACE CONDITIONS

贪了杯 2024-09-23 19:11:59

我没有看到任何与错误消息相关的明显错误。

但是,如果 SYSQLSequence 是一个序列,那么直接更新它是不常见的(至少),并且在某些 Oracle 版本中可能不允许。我相信你必须做这样的事情:

alter sequence SYSQLSequence
increment by 4;

更新:好吧,这也有缺陷,所以我把它改掉了。

I don't see anything obviously wrong which relates to the error message.

However, if SYSQLSequence is a sequence, then directly updating it is unusual—at least—and maybe not allowed in certain Oracle versions. I believe you have to do something like this instead:

alter sequence SYSQLSequence
increment by 4;

Update: okay, that's flawed too, so I've scratched it out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文