Subject | Re: Problem with before insert trigger defaults and insert statements |
---|---|
Author | Adam |
Post date | 2005-09-28T00:28:11Z |
--- In firebird-support@yahoogroups.com, "m.vanzeist"
<m.vanzeist@h...> wrote:
You can't do it both ways. If you create a default value as a
constant 0, then the field will have the value 0 when you run an
insert statement without referencing that field.
You have not provided a single example in your problem where you want
it to default to 0?
It seems to me that you should just declare it as an integer field,
and then handle within your trigger setting the default value. Like
this:
if (new.field2 IS NULL) then
begin
if (:VDEF1 IS NULL) then
begin
NEW.FIELD2 =:V_DEF1;
end
else
begin
NEW.FIELD2 =0;
end
end
Adam
<m.vanzeist@h...> wrote:
> Hi All,inserted
>
> I am having difficulty distinguishing between a value that has been
> in contrast to a default value.NEW.FIELD1
>
> Let me clarify.
>
> I use 1 domain
> Domain:MYDOMAIN
> integer
> default 0
>
>
> I have a simple table containing 4 columns
>
> Table:MYTABLE
> 1 ID Integer
> 2 Field1 Integer
> 3 Field2 MYDOMAIN
> 4 Field3 VARCHAR(50)
>
> my before insert trigger is like
> AS
> DECLARE VARIABLE V_DEF1 INTEGER;
> DECLARE VARIABLE V_DEF2 VARCHAR(50);
> BEGIN
> if (new."ID" is null) then new."ID" = gen_id(NEWID,1);
> if ((new.Field1 IS NOT NULL) then begin
> SELECT DEFVALUE1,DEFVALUE2 FROM DEFAULTVALUES WHERE ID =
> INTO :V_DEF1,:V_DEF2;(Which are
> if NEW.FIELD2 is null then
> NEW.FIELD2 =:V_DEF1
> if NEW.FIELD3 is null then
> NEW.FIELD3 =:V_DEF2
> end
> END
>
> in the before insert trigger i want to set up some default values
> copied from another table and using the integer for Field1 as anindex), but
> i want to set these default values only when the INSERT statementhas not
> inserted values for specific fields (Lets take Field2 and Field3for this
> example)because it
> Field ID is always populated with a generator.
>
> My problem is that i cannot detect if Field2 has been inserted
> will not be NULL due to the fact that the domain has a defaultthe
> I could get around this by not providing a default ofcourse and set
> default value myself in the trigger, or checking if the value forField2 <>0
> (Which i don't want becaus a user could legitimatly insert a 0 hereand then
> the default value must NOT be set)Field1
>
> but is there another way of detecting if a field has been explicitly
> inserted eg INSERT Field1,Field2 values(1,3) instead of INSERT
> values(1)Mario,
>
> Best regards, mario van Zeist.
>
You can't do it both ways. If you create a default value as a
constant 0, then the field will have the value 0 when you run an
insert statement without referencing that field.
You have not provided a single example in your problem where you want
it to default to 0?
It seems to me that you should just declare it as an integer field,
and then handle within your trigger setting the default value. Like
this:
if (new.field2 IS NULL) then
begin
if (:VDEF1 IS NULL) then
begin
NEW.FIELD2 =:V_DEF1;
end
else
begin
NEW.FIELD2 =0;
end
end
Adam