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.
Static global variable is always declared outside the main function while the static local variable is declared inside the main or any block element (for example inside a function, inside a loop etc.).
It is not possible to declare global variables in SQL Server. Sql server has a concept of global variables, but they are system defined and can not be extended.
User-defined global variables let users extend the functionality of the database management system by adding their own or third-party vendor variable definitions. A user-defined global variable is created using the CREATE VARIABLE statement, and registered to the database manager in the catalog.
A user-defined schema global variable is created by using the CREATE VARIABLE SQL statement. A user-defined module global variable is created using the ADD VARIABLE or PUBLISH VARIABLE option of the ALTER MODULE SQL statement.
I guess this will work the best for me:
CREATE FUNCTION [dbo].[fn_GetDefaultPercent]()
RETURNS decimal(5,4)
AS
BEGIN
RETURN 1.0000
END
You can have a look at something like this
"Global variables" in SQL Server
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With