mysql - SQL variable in IF exists -
i can't figure out correct way assign query output variable later use in insert clause.
the error
error 1064 (42000): have error in sql syntax; check manual corresponds mysql server version right syntax use near 'set @lennuid = (select lend_id lend sihtpunkt=in_kuhu , kuupaev=in_' @ line 8
i've tried looking @ every guide on variables in sql none of them me solve this.
iam using below code:
create procedure proov( in_kuhu varchar(50), in_nimi1 varchar(50), in_nimi2 varchar(50), in_adre varchar(50), in_telo varchar(20), in_email varchar(100), in_date date) begin declare viga integer default 0 ; declare lennuid int; start transaction; if exists ( set @lennuid = (select lend_id lend sihtpunkt=in_kuhu , kuupaev=in_date)) insert broneering (lend_id, bron_aeg, eesnim, perenimi, aadress, telefon, email) values (lennuid, now(), in_nimi1, in_nimi2, in_adre, in_telo, in_email); else set viga=1; select 'muudatus ebaonnestus ',viga; end if; if viga=0 commit; select 'korras'; else select 'tagasi'; rollback; end if;
you might want use select into
:
select lend_id @lennuid lend sihtpunkt=in_kuhu , kuupaev=in_date;
alternatively:
select @lennuid := lend_id lend sihtpunkt=in_kuhu , kuupaev=in_date;
either way works same, can use variable like...
in particular example, though, problem how exists
being used along set
. fix this, use following:
if exists (select lend_id lend sihtpunkt=in_kuhu , kuupaev=in_date) ...
what problem here is, setting variable set
inside of exists
check.
or select first count
numeric variable:
declare cnt integer; select count(lend_id) @cnt lend sihtpunkt=in_kuhu , kuupaev=in_date; if @cnt > 0 end if;
Comments
Post a Comment