Create a global static variable in SQL Server?

只愿长相守 提交于 2019-12-18 05:49:06

问题


Is there a way to create a global variable in SQL Server, in such a way that it persists even if the server is restarted, so I can use it in functions?

Example from what I need:

DECLARE @@DefaultValue bit

This variable should never be deleted unless I explicityl do so.


回答1:


I guess this will work the best for me:

CREATE FUNCTION [dbo].[fn_GetDefaultPercent]()
RETURNS decimal(5,4)
AS
BEGIN
    RETURN 1.0000
END



回答2:


You can have a look at something like this

"Global variables" in SQL Server




回答3:


Not a global variable.

There's chance you can define a global UDF like you can create a "system" stored proc (starts "sp" in master), but I've not tried it.

Note:

Even DECLARE @@DefaultValue bit is actually local:

@ means local variable, identifier is @DefaultValue

It's not really global: try SELECT @@DefaultValue from 2 another query window




回答4:


I know is answered but just for fun :)

How about a table with 2 columns like:

GLB_VARIABLES:
GLB_VAR_NAME varchar(100) PRIMARY KEY,
GLB_VAR_VALUE varchar(100)


来源:https://stackoverflow.com/questions/1838429/create-a-global-static-variable-in-sql-server

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