Firebird: Using global variable

大城市里の小女人 提交于 2019-12-10 17:48:48

问题


I would like to assign a value to a variable which is used in an sql code in firebird. The MySQL-Code would be:

SET @x = 1;
SELECT @x;

What is the correspondent Firebird-Code?

Thanks for help.


回答1:


To define a user-defined session-specific variable in Firebird you can use rdb$set_context.

The correspondent Firbird-code for your MySql-example would be:

select rdb$set_context('USER_SESSION', 'x', 1) from rdb$database
select rdb$get_context('USER_SESSION', 'x') from rdb$database

Notes:

1.) Be aware that variable names are case-sensitive.

2.) Internally variable values are stored with datatype VARCHAR(255) and thus casted to VARCHAR(255)!!

3.) The maximum number of variables is 1000.

4.) You don't need to refer to rdb$database:

select rdb$get_context('USER_SESSION', 'x') from some_table_name

would work as well.




回答2:


I'm no expert in Firebird, but I believe it would be something like this...

set term ^ ;

EXECUTE BLOCK
AS
DECLARE VARIABLE x int;
BEGIN
  x = 1; 
  --do whatever you want with x, there's no such thing 
  --as to select the variable value to print it
END
^

set term ; ^


来源:https://stackoverflow.com/questions/15559173/firebird-using-global-variable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!