在 SQL Server 中创建全局静态变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1838429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create a global static variable in SQL Server?
提问by Shimmy Weitzhandler
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?
有没有办法在 SQL Server 中创建一个全局变量,这样即使服务器重新启动它也会持续存在,所以我可以在函数中使用它?
Example from what I need:
我需要的示例:
DECLARE @@DefaultValue bit
This variable should never be deleted unless I explicityl do so.
除非我明确这样做,否则永远不应删除此变量。
回答by Shimmy Weitzhandler
I guess this will work the best for me:
我想这对我来说最有效:
CREATE FUNCTION [dbo].[fn_GetDefaultPercent]()
RETURNS decimal(5,4)
AS
BEGIN
RETURN 1.0000
END
回答by Adriaan Stander
回答by gbn
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.
您有机会定义一个全局 UDF,就像您可以创建一个“系统”存储过程(在 master 中启动“sp”),但我还没有尝试过。
Note:
笔记:
Even DECLARE @@DefaultValue bit
is actually local:
EvenDECLARE @@DefaultValue bit
实际上是本地的:
@
means local variable, identifier is @DefaultValue
@
表示局部变量,标识符是 @DefaultValue
It's not really global: try SELECT @@DefaultValue
from 2 another query window
它不是真正的全局:尝试SELECT @@DefaultValue
从 2 另一个查询窗口
回答by Zberteoc
I know is answered but just for fun :)
我知道有人回答但只是为了好玩:)
How about a table with 2 columns like:
一个有 2 列的表格怎么样:
GLB_VARIABLES:
GLB_VAR_NAME varchar(100) PRIMARY KEY,
GLB_VAR_VALUE varchar(100)