3

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?

4

1 回答 1

0

为什么不返回更新记录的 id 及其新状态?

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

然后在查询中使用它:

SELECT a.*, p.status FROM tbl1 a LEFT JOIN proc_getstatus(1) p ON a.id = p.id
于 2012-11-28T08:47:27.130 回答