tags:

views:

107

answers:

2

Is it possible to have database wide constants? What I want is to define a constant like:

  • UPDATE_CONSTANT = 1
  • INSERT_CONSTANT = 2
  • DELETE_CONSTANT = 3

and then use it in for example a trigger like:

CREATE TRIGGER AD_PRJ_PROJECTS FOR PRJ_PROJECT
ACTIVE AFTER DELETE
POSITION 1
AS
BEGIN
   EXECUTE PROCEDURE SP_ADD_HISTORY 'PRJ_PROJECT', DELETE_CONSTANT;
END;
+2  A: 

You could use a generator:

SET GENERATOR DELETE_CONSTANT TO 3;

...

EXECUTE PROCEDURE SP_ADD_HISTORY 'PRJ_PROJECT', GEN_ID(DELETE_CONSTANT, 0);
mjustin
That's sneaky. :-)
robsoft
I'll accept this answer, however using generators and calling them with GEN_ID() defeat the pourpose of the "constant" idea of been easier the read/use/maintain.The correct answer should be: It's not possible to get it straightforward.
Daniel Luyo
thanks :-) ... another solution could be a UDF with the name DELETE_CONSTANT() which returns the constant value ... this is no perfect answer either ;) ...
mjustin
Generators are not constants. Do not use them as such. If for any one reason this generator is modified you can have a whole bunch of code which suddenly throws out incorrect answers. It might be a long time before you find the problem.
Edelcom
A: 

I don't think there is an easy way for declaring constants.

I could be done by creating you own DLL for user defined function, and lmake a function for each constant.

I Think the Idea using generators as "global" constants is briliant.

But you can make a "local constant" to make your code a bit more readable:

CREATE TRIGGER AD_PRJ_PROJECTS FOR PRJ_PROJECT
ACTIVE AFTER DELETE
POSITION 1
AS
  DECLARE VARIABLE DELETE_CONSTANT INTEGER;
BEGIN
   DELETE_CONSTANT = 1;
   EXECUTE PROCEDURE SP_ADD_HISTORY 'PRJ_PROJECT', DELETE_CONSTANT;
END;
BennyBechDk
In lates release you can write: DECLARE VARIABLE DELETE_CONSTANT INTEGER = 1;
Andrei K.
It is from Firebird 1.5 that variabeles can be initialized
BennyBechDk