tags:

views:

72

answers:

2

Hi Folks! 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:
+2  A: 

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;
OMG Ponies
Thanks OMG Ponie..What about this statement: (SELECT Stay_Id from MVStay where StayNumber = 'xxxx' AND StayState = 2)
muddu83
OMG Ponies
+2  A: 

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...

Alex Poole
+1: For verbatim translation
OMG Ponies
@OMG I was bored...
Alex Poole
Yes I am a DBA and now trying to grab some basic plsql stuff! ;-) Anyways appreciate your help!We use date and time stamps as string format in our schema. I want to just enter Staynumber, current date, and stay_workflow_id, rest it needs to be done via the code..
muddu83
sorry a silly question, what is %type? do i have to mention the data type like varchar2, num etc here?
muddu83
The `%TYPE` means the variable is declared as the same type as the `table.column`. You can declare it explicitly, but using `%TYPE` means that (a) you don't necessarily need to know the exact type to get your query working; (b) won't get it slightly wrong - say a char too small - which might not show up until a runtime error much later; and (c) won't have to update the code if the matching table column definition changes later.
Alex Poole
OMG Ponies
I ran your code, but i get this Error report, please check my question which I have edited too for details...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 <an exponent (**)> <> or != or ~= >= <= <> and or like like2 like4 likec between || multiset member submultisetThe symbol ";" was substituted for "IF" to continue.06550. 00000 - "line %s, column %s:\n%s"*Cause: Usually a PL/SQL compilation error.*Action:
muddu83
Well with your updated code the error tells you that it's expecting one of the listed characters somewhere before the 'if'. This usually means there's something wrong with the preceding statement; ignoring the comments that means the one setting l_stay_workflow_id. And lo, that doesn't have a `;` at the end.
Alex Poole
Thanks everyone!! :)
muddu83