Subject | Re: Select + Update in a single SPROC? |
---|---|
Author | hartertobak |
Post date | 2007-11-23T13:32:16Z |
Hi,
well my gui does´nt support the set term statement (EMS SQL Manager),
but nevertheless I got it almost working:
CREATE PROCEDURE SP_TBLONE_INS_UPD(
SEARCHTERM VARCHAR(400) CHARACTER SET UTF8 DEFAULT NULL)
RETURNS(
SEARCHEXISTS SMALLINT)
AS
DECLARE VARIABLE getid INTEGER;
DECLARE VARIABLE idcount INTEGER;
BEGIN
getid = NULL;
idcount = null;
SELECT ID, SEARCHCOUNT
FROM TBLONE
WHERE SEARCHTERM = :SEARCHTERM
INTO :getid, :idcount;
IF ( getid IS NULL ) THEN
BEGIN
INSERT INTO TBLONE
( SEARCHTERM, LASTSEARCH, SEARCHCOUNT)
VALUES
(:SEARCHTERM,cast('now' as timestamp), 1 );
end
if (getid IS NOT NULL) then
begin
idcount = idcount + 1;
update TBLONE
SET SEARCHCOUNT = :idcount
where ID = :getid;
END
SUSPEND;
END;
Now the following happens:
I the getid is null both conditions seem to be true, because the
insert AND the update path will be followed... :(
well my gui does´nt support the set term statement (EMS SQL Manager),
but nevertheless I got it almost working:
CREATE PROCEDURE SP_TBLONE_INS_UPD(
SEARCHTERM VARCHAR(400) CHARACTER SET UTF8 DEFAULT NULL)
RETURNS(
SEARCHEXISTS SMALLINT)
AS
DECLARE VARIABLE getid INTEGER;
DECLARE VARIABLE idcount INTEGER;
BEGIN
getid = NULL;
idcount = null;
SELECT ID, SEARCHCOUNT
FROM TBLONE
WHERE SEARCHTERM = :SEARCHTERM
INTO :getid, :idcount;
IF ( getid IS NULL ) THEN
BEGIN
INSERT INTO TBLONE
( SEARCHTERM, LASTSEARCH, SEARCHCOUNT)
VALUES
(:SEARCHTERM,cast('now' as timestamp), 1 );
end
if (getid IS NOT NULL) then
begin
idcount = idcount + 1;
update TBLONE
SET SEARCHCOUNT = :idcount
where ID = :getid;
END
SUSPEND;
END;
Now the following happens:
I the getid is null both conditions seem to be true, because the
insert AND the update path will be followed... :(