Quantcast
Channel: SCN: Message List - SAP HANA Developer Center
Viewing all articles
Browse latest Browse all 9165

Re: Before insert Triggers in SAP HANA

$
0
0

Hi Micha

 

you're right and I was wrong on this.

 

It's even in the SQL standard that before triggers have half the transition variables (the NEW values).

 

Running with SPS 11 even SAP HANA understands this now:

 

create column table colors (code_id bigint, red int, green int, blue int, limit_256 tinyint)
-- table contains color codes and the color parts in red, green and blue
-- if limit_256 is set to -1 or 1 then the values stored can only be between 0 and 255
drop trigger CHECK_INS;
create trigger check_ins before insert
on colors 
referencing NEW row as new
for each row
begin
declare new_id integer = -1; 
declare VALUE_OUT_OF_RANGE_ERROR condition for SQL_ERROR_CODE 19999;
declare EXIT HANDLER FOR VALUE_OUT_OF_RANGE_ERROR RESIGNAL;        if abs(:new.limit_256) > 0 then /* any value other than 0 will be considered TRUE*/           if  not (   (:new.red >= 0 and :new.red <= 255)              and (:new.green >= 0 and :new.green <= 255)              and (:new.blue >= 0 and :new.blue <=255)              )       then               signal VALUE_OUT_OF_RANGE_ERROR                  SET MESSAGE_TEXT = 'Invalid color value for 256 limit (R, G, B): ('                                    ||:new.red|| ', '||:new.green|| ', '||:new.blue||')';                              end if;    end if;
/* this is a really bad idea for mass inserts! */    if :new.code_id <0 then        select max(code_id)+1 into new_id  from colors;        new.code_id = :new_id;    end if;
end;
truncate table colors;
-- the following all work
insert into colors values (1, -50, 500, 50, 0);
insert into colors values (2, 0, 0, 0, 1);
insert into colors values (3, -0, -0, -0, 1);
insert into colors values (4, 255, 255, 255, 1);
insert into colors values (-1, 30, 30, 30, 1);
insert into colors values (-1, 120, 120, 120, 1);
insert into colors values (-1, 240, 240, 240, 1);
-- the following all fail
insert into colors values (-1, 256, 0, 0, 1);
insert into colors values (-1, 0, 266, 0, 1);
insert into colors values (-1, 0, 0, -266, 1);
insert into colors values (-1, 50, 50, -150, 1);
select * from colors;

Thanks for pointing out my product-bias-induced-logic-resistance!

 

Cheers,

Lars


Viewing all articles
Browse latest Browse all 9165

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>