Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Data Studio - Setting SQL variables to be used as globals

In Azure Data Studio (ADS), is it possible to reuse SQL parameters from query to query? Not sure if I'm jumping out of the intended purpose of ADS but it would be really great if I could declare a set of variables in one code text (or anywhere) and have all my queries understand and utilize them. Something similar to Jupyter notebooks with Python, how you could do the global variables in one code block and all others would respect those variables.

enter image description here

In general, I am having little luck finding documentation on ADS other than the official Microsoft docs.

like image 960
Volearix Avatar asked Nov 25 '19 14:11

Volearix


People also ask

How do you declare a global variable in SQL?

You cannot declare global variables in SQLServer. If you're using Management Studio you can use SQLCMD mode like @Lanorkin pointed out. Otherwise you can use CONTEXT_INFO to store a single variable that is visible during a session and connection, but it'll disappear after that.

Does SQL have global variables?

A global variable is a named memory variable that you access through SQL statements. Global variables let you share relational data between SQL statements without the need for application logic to support this data transfer.

Can you assign variables in the declare statement?

Variables in SQL procedures are defined by using the DECLARE statement. Values can be assigned to variables using the SET statement or the SELECT INTO statement or as a default value when the variable is declared. Literals, expressions, the result of a query, and special register values can be assigned to variables.

How do I use a variable in SQL SELECT statement?

The syntax for assigning a value to a SQL variable within a SELECT query is @ var_name := value , where var_name is the variable name and value is a value that you're retrieving. The variable may be used in subsequent queries wherever an expression is allowed, such as in a WHERE clause or in an INSERT statement.


2 Answers

As far as I know, no, because variables are in the scope of the current batch.

A workaround would be to create a temp table and insert that value. It's really gross, but it works.

In code cell #1:

CREATE TABLE #variableStorage (varname VARCHAR(100), val VARCHAR(100))
INSERT INTO #variableStorage
VALUES 
('SomeVariable', 'Foo')

CREATE TABLE #testing (ID INT, testval VARCHAR(100))

INSERT INTO #testing
VALUES 
(100, 'Foo')

In code cell #2:

SELECT *
from #testing

WHERE testval = (
    SELECT val 
    FROM #variableStorage 
    WHERE varname = 'SomeVariable'
    )
like image 79
dfundako Avatar answered Oct 17 '22 13:10

dfundako


There is no way to declare a global variable in Transact-SQL. So We also can not set global variables with ADS.

In earlier version:

  • The names of some Transact-SQL system functions begin with two at signs (@@). Although in earlier versions of SQL Server, the @@functions are referred to as global variables, they are not variables and do not have the same behaviors as variables. The @@functions are system functions, and their syntax usage follows the rules for functions.

Reference: Variables (Transact-SQL).

But there is another way can help you set the variable as the global variables with SSMS SQLCMD Mode. enter image description here

You can reference this blog:how to declare global variable in SQL Server..?

Hope this helps.

like image 2
Leon Yue Avatar answered Oct 17 '22 12:10

Leon Yue