需要创建 pl/sql 查询的帮助

发布于 2024-09-17 07:21:38 字数 2281 浏览 3 评论 0 原文

向大家问好! 我想通过声明以下变量来创建 pl/sql 查询,例如:

:stay_id = (SELECT Stay_Id from MVStay where StayNumber = 'xxxx' AND StayState = 2);
-- get passage linked to the stay and is 'discharged'
:passage_id = (SELECT Passage_Id from MVStayWorkflow where Stay_Id = :stay_id and WorkflowAction = 31);

-- get current date
:now = to_char(sysdate, 'YYYYMMDD HH:MI:SS.FF')
-- get a new sequence number
:stay_workflow_id = (get it from the concerned table)

--insert ‘Admin discharged’ workflow step
if( passage_id is not NULL)
begin
  Insert into MVStayWorkflow (StayWorkflow_Id, Stay_Id, Passage_Id, User_Id, RespUnit_Id, Resource_Id,
  WorkflowAction, CurrentState, PreviousState, WorkflowTime, UserStamp, TimeStamp)
  values (:stay_workflow_id, :stay_id, :passage_id, 1, 0, 0, 11, 7, 7, :now, 1, :now)
end

问候 Mohammed

你好,Alex..

我使用了你的代码,但遇到了错误: 从命令中的第 3 行开始出错: 宣布 l_stay_id MVStay.Stay_Id%TYPE; l_passage_id MVStayWorkflow.Passage_Id%TYPE; l_stay_workflow_id MVStayWorkflow.Stay_Workflow_Id%TYPE; l_now MVSTAY.ENDDATETIME%TYPE; begin

/* 关闭stay,stay_number = '030074559' */ 选择 Stay_Id 进入 l_stay_id 来自 MVStay 其中住宿编号 = '030074559' 且保持状态 = 2;

/* get passage linked to the stay and is 'discharged' */
select Passage_Id into l_passage_id
from MVStayWorkflow
where Stay_Id = l_stay_id
and WorkflowAction = 31;

/* get current date types in MVStayWorkflow? */
l_now := to_char(sysdate, 'YYYYMMDD HH:MI:SS.FF');

/* get a new sequence number */
l_stay_workflow_id := 500000

/* insert ‘Admin discharged’ workflow step */
if passage_id is not NULL then
    insert into MVStayWorkflow (StayWorkflow_Id, Stay_Id, Passage_Id,
        User_Id, RespUnit_Id, Resource_Id, WorkflowAction, CurrentState,
        PreviousState, WorkflowTime, UserStamp, TimeStamp)
    values (l_stay_workflow_id, l_stay_id, l_passage_id,
        1, 0, 0, 11, 7, 7, l_now, 1, l_now);
end if;

结尾; 错误报告: ORA-06550: 第 27 行,第 5 列: PLS-00103:在期望以下其中一项时遇到符号“IF”:

  • & = - + ; < //> at in 是 mod 余数不是 rem <>或 != 或 ~= >= <= <>和或喜欢像2 like4 likec || 之间多重集成员子多重集 符号“;”替换为“IF”继续。
  • 00000 - “第 %s 行,第 %s 列:\n%s” *原因:通常是 PL/SQL 编译错误。 *行动:

Greetings to all!
I want to create a pl/sql query by declaring variables for the following eg:

:stay_id = (SELECT Stay_Id from MVStay where StayNumber = 'xxxx' AND StayState = 2);
-- get passage linked to the stay and is 'discharged'
:passage_id = (SELECT Passage_Id from MVStayWorkflow where Stay_Id = :stay_id and WorkflowAction = 31);

-- get current date
:now = to_char(sysdate, 'YYYYMMDD HH:MI:SS.FF')
-- get a new sequence number
:stay_workflow_id = (get it from the concerned table)

--insert ‘Admin discharged’ workflow step
if( passage_id is not NULL)
begin
  Insert into MVStayWorkflow (StayWorkflow_Id, Stay_Id, Passage_Id, User_Id, RespUnit_Id, Resource_Id,
  WorkflowAction, CurrentState, PreviousState, WorkflowTime, UserStamp, TimeStamp)
  values (:stay_workflow_id, :stay_id, :passage_id, 1, 0, 0, 11, 7, 7, :now, 1, :now)
end

Regards
Mohammed

Hi Alex..

I used your code but encountered an error:
Error starting at line 3 in command:
declare
l_stay_id MVStay.Stay_Id%TYPE;
l_passage_id MVStayWorkflow.Passage_Id%TYPE;
l_stay_workflow_id MVStayWorkflow.Stay_Workflow_Id%TYPE;
l_now MVSTAY.ENDDATETIME%TYPE;
begin

/* get closed stay having stay_number = '030074559' */
select Stay_Id into l_stay_id
from MVStay
where StayNumber = '030074559'
and StayState = 2;

/* get passage linked to the stay and is 'discharged' */
select Passage_Id into l_passage_id
from MVStayWorkflow
where Stay_Id = l_stay_id
and WorkflowAction = 31;

/* get current date types in MVStayWorkflow? */
l_now := to_char(sysdate, 'YYYYMMDD HH:MI:SS.FF');

/* get a new sequence number */
l_stay_workflow_id := 500000

/* insert ‘Admin discharged’ workflow step */
if passage_id is not NULL then
    insert into MVStayWorkflow (StayWorkflow_Id, Stay_Id, Passage_Id,
        User_Id, RespUnit_Id, Resource_Id, WorkflowAction, CurrentState,
        PreviousState, WorkflowTime, UserStamp, TimeStamp)
    values (l_stay_workflow_id, l_stay_id, l_passage_id,
        1, 0, 0, 11, 7, 7, l_now, 1, l_now);
end if;

end;
Error report:
ORA-06550: line 27, column 5:
PLS-00103: Encountered the symbol "IF" when expecting one of the following:

  • & = - + ; < / > at in is mod remainder not rem
    <> or != or ~= >= <= <> and or like like2
    like4 likec between || multiset member submultiset
    The symbol ";" was substituted for "IF" to continue.

    1. 00000 - "line %s, column %s:\n%s"
      *Cause: Usually a PL/SQL compilation error.
      *Action:

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

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

发布评论

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

评论(2

无人接听 2024-09-24 07:21:38

问题似乎是您想学习 PL/SQL,而不是帮助查询。这里有数据类型的各种问题,但基本轮廓如下:

declare
    l_stay_id MVStay.Stay_Id%TYPE;
    l_passage_id MVStayWorkflow.Passage_Id%TYPE;
    l_stay_workflow_id MVStayWorkflow.Stay_Workflow_Id%TYPE;
    l_now varchar2(20); /* but why isn't this a date? */
begin
    select Stay_Id into l_stay_id
    from MVStay
    where StayNumber = 'xxxx' /* number or string? */
    and StayState = 2;

    /* get passage linked to the stay and is 'discharged' */
    select Passage_Id into l_passage_id
    from MVStayWorkflow
    where Stay_Id = l_stay_id
    and WorkflowAction = 31;

    /* get current date - really, why hold it as a string? what are the field
       types in MVStayWorkflow? */
    l_now := to_char(sysdate, 'YYYYMMDD HH:MI:SS.FF');

    /* get a new sequence number */
    l_stay_workflow_id := (get it from the concerned table)
    /* or, select ... into; or use a proper sequence for the insert? */

    /* insert ‘Admin discharged’ workflow step */
    if passage_id is not NULL then
        insert into MVStayWorkflow (StayWorkflow_Id, Stay_Id, Passage_Id,
            User_Id, RespUnit_Id, Resource_Id, WorkflowAction, CurrentState,
            PreviousState, WorkflowTime, UserStamp, TimeStamp)
        values (l_stay_workflow_id, l_stay_id, l_passage_id,
            1, 0, 0, 11, 7, 7, l_now, 1, l_now);
    end if;
end;

您需要了解每个部分在做什么,并阅读 SQL 和 PL/SQL 之间的差异...

Question seems to be that you want to learn PL/SQL, rather than help with a query. Various issues with datatypes in here but the basic outline would be something like:

declare
    l_stay_id MVStay.Stay_Id%TYPE;
    l_passage_id MVStayWorkflow.Passage_Id%TYPE;
    l_stay_workflow_id MVStayWorkflow.Stay_Workflow_Id%TYPE;
    l_now varchar2(20); /* but why isn't this a date? */
begin
    select Stay_Id into l_stay_id
    from MVStay
    where StayNumber = 'xxxx' /* number or string? */
    and StayState = 2;

    /* get passage linked to the stay and is 'discharged' */
    select Passage_Id into l_passage_id
    from MVStayWorkflow
    where Stay_Id = l_stay_id
    and WorkflowAction = 31;

    /* get current date - really, why hold it as a string? what are the field
       types in MVStayWorkflow? */
    l_now := to_char(sysdate, 'YYYYMMDD HH:MI:SS.FF');

    /* get a new sequence number */
    l_stay_workflow_id := (get it from the concerned table)
    /* or, select ... into; or use a proper sequence for the insert? */

    /* insert ‘Admin discharged’ workflow step */
    if passage_id is not NULL then
        insert into MVStayWorkflow (StayWorkflow_Id, Stay_Id, Passage_Id,
            User_Id, RespUnit_Id, Resource_Id, WorkflowAction, CurrentState,
            PreviousState, WorkflowTime, UserStamp, TimeStamp)
        values (l_stay_workflow_id, l_stay_id, l_passage_id,
            1, 0, 0, 11, 7, 7, l_now, 1, l_now);
    end if;
end;

You need to understand what each part is doing though, and read up on the differences between SQL and PL/SQL...

最后的乘客 2024-09-24 07:21:38

在 Oracle 中,您使用 SELECT ... INTO ... 语法填充变量:

DECLARE v_workflow_id NUMBER;

BEGIN

  SELECT ct.workflow_id
    INTO v_workflow_id 
    FROM CONCERNED_TABLE ct;

  INSERT INTO MVSTAYWORKFLOW
    (StayWorkflow_Id, Stay_Id, Passage_Id, User_Id, RespUnit_Id, Resource_Id, WorkflowAction, CurrentState, PreviousState, WorkflowTime, UserStamp, TimeStamp)
    SELECT v_workflow_id, s.stay_id, smf.passage_id, 1, 0, 0, 11, 7, 7, TO_CHAR(SYSDATE, 'YYYYMMDD HH:MI:SS.FF'), 1, TO_CHAR(SYSDATE, 'YYYYMMDD HH:MI:SS.FF')
     FROM MVSTAY s
LEFT JOIN MVSTAYWORKFLOW smf ON smf.stay_id = s.stay_id
                          AND smf.workflowaction = 31
  WHERE smf.passage_id IS NOT NULL
    AND s.stayNumber = 'xxxx' 
    AND s.staystate = 2;

END;

此外,Oracle 将以冒号(“:”)为前缀的文本解释为 BIND 变量,这意味着它应该是用传入参数值填充。

如果您希望将stay_id 与我提供的分开,请使用:

DECLARE your_variable_name MYSTAY.stay_id%TYPE;

SELECT s.stay_id 
  INTO your_variable_name
  FROM MVSTAY s 
 WHERE s.stayNumber = 'xxxx' 
   AND s.staystate = 2;

In Oracle, you populate variables using the SELECT ... INTO ... syntax:

DECLARE v_workflow_id NUMBER;

BEGIN

  SELECT ct.workflow_id
    INTO v_workflow_id 
    FROM CONCERNED_TABLE ct;

  INSERT INTO MVSTAYWORKFLOW
    (StayWorkflow_Id, Stay_Id, Passage_Id, User_Id, RespUnit_Id, Resource_Id, WorkflowAction, CurrentState, PreviousState, WorkflowTime, UserStamp, TimeStamp)
    SELECT v_workflow_id, s.stay_id, smf.passage_id, 1, 0, 0, 11, 7, 7, TO_CHAR(SYSDATE, 'YYYYMMDD HH:MI:SS.FF'), 1, TO_CHAR(SYSDATE, 'YYYYMMDD HH:MI:SS.FF')
     FROM MVSTAY s
LEFT JOIN MVSTAYWORKFLOW smf ON smf.stay_id = s.stay_id
                          AND smf.workflowaction = 31
  WHERE smf.passage_id IS NOT NULL
    AND s.stayNumber = 'xxxx' 
    AND s.staystate = 2;

END;

Also, Oracle interprets text prefixed with a colon (":") to be a BIND variable, meaning it expects to be populated with an incoming parameter value.

If you want the stay_id separate from what I provided, use:

DECLARE your_variable_name MYSTAY.stay_id%TYPE;

SELECT s.stay_id 
  INTO your_variable_name
  FROM MVSTAY s 
 WHERE s.stayNumber = 'xxxx' 
   AND s.staystate = 2;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文