Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a global static variable in SQL Server?

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.

like image 986
Shimmy Weitzhandler Avatar asked Dec 03 '09 08:12

Shimmy Weitzhandler


People also ask

How do you declare a global static variable?

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.).

How do I create a global variable in SQL Server?

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.

Can you create your own global variables in SQL?

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.

How do you create a global variable in a database?

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.


2 Answers

I guess this will work the best for me:

CREATE FUNCTION [dbo].[fn_GetDefaultPercent]()
RETURNS decimal(5,4)
AS
BEGIN
    RETURN 1.0000
END
like image 52
Shimmy Weitzhandler Avatar answered Sep 24 '22 05:09

Shimmy Weitzhandler


You can have a look at something like this

"Global variables" in SQL Server

like image 20
Adriaan Stander Avatar answered Sep 24 '22 05:09

Adriaan Stander