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

时间:2010-08-29 19:44:20

标签: sql oracle plsql

向所有人致以问候! 我想通过声明以下变量来创建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

此致 穆罕默德

嗨,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; 开始

/ *关闭逗留有stay_number ='030074559'* /     选择Stay_Id到l_stay_id     来自MVStay     其中StayNumber ='030074559'     和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;

端; 错误报告: ORA-06550:第27行,第5栏: PLS-00103:遇到以下任何一种情况时遇到符号“IF”:

  • &安培; = - +; < />在in是mod余数而不是rem  <>或!=或〜=> =< =<>或者喜欢喜欢2 like | likec之间的|| multiset成员submultiset 符号“;”取代“IF”继续。
    1. 00000 - “行%s,列%s:\ n%s” *原因:通常是PL / SQL编译错误。 *操作:

2 个答案:

答案 0 :(得分:2)

在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;

答案 1 :(得分:2)

问题似乎是你想要学习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之间的差异......