Need help with creating a pl/sql query
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; 开发者_StackOverflow中文版 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.
- 00000 - "line %s, column %s:\n%s" *Cause: Usually a PL/SQL compilation error. *Action:
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...
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;
精彩评论