简体   繁体   中英

Select after update in a stored procedure

I am trying to write procedure without using directly lock statements.

SET TERM ^ ;
ALTER PROCEDURE PROC_GETSTATUS (
    IDTBL1 Integer )
RETURNS (
    STATUS Varchar(255) )
AS
declare variable t int;

BEGIN

   t=gen_id(RPUSING,1);
   update  TBL1 a set a.STATUS=cast('USINGBY' as varchar(255))||cast(:t as varchar(255)) where a.STATUS='free' and a.ID=:IDTBL1 order by a.LASTUPDATED rows 1 to 1;

   STATUS=cast('USINGBY' as varchar(255))||cast(:t as varchar(255));
   SUSPEND;
   END^
SET TERM ; ^


GRANT EXECUTE
 ON PROCEDURE PROC_GETSTATUS TO  SYSDBA;

When I'm selecting data from this by query like:

select * from TBL1 a where a.STATUS in (select b.STATUS from PROC_GETSTATUS(1));

It returns null. But this select

select * from TBL1 a where a.STATUS like '%USINGBY%'

in current transaction returns updated data. How to rewrite this query by one select to procedure in current transaction?

Why not return id of updated record and its new status?

CREATE OR ALTER PROCEDURE PROC_GETSTATUS (
  IDTBL1 INTEGER)
RETURNS (
  ID INTEGER,
  STATUS VARCHAR(255))
AS
BEGIN
  FOR
    SELECT FIRST 1 a.id 
    FROM tbl1 a
    WHERE a.status='free' AND a.id=:IDTBL1 
    ORDER BY a.lastupdated 
    INTO :ID
  DO BEGIN
    STATUS = 'USINGBY' || gen_id(RPUSING,1); 
    UPDATE tbl1 a SET a.status = :STATUS
      WHERE a.id = :ID;
    SUSPEND;
  END 
END

Then use it in the query:

SELECT a.*, p.status FROM tbl1 a LEFT JOIN proc_getstatus(1) p ON a.id = p.id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM