Tutorial :how to declare global variable in sql



Question:

I would like create a GLOBAL VARIABLE in a Sql script. For my understanding using DECLARE I will be able to create just LOCAL variable.

Any idea how to create theem?

thanks


Solution:1

Assuming MS SQL Server, and assuming the MS definition of 'Global Variable' you cannot. If you need a variable to be accessable across multiple stored procedures, or ad-hoc queries, you will need some other way to hold the data: say a table which holds the variable for you.

Note: the hyperlink does not go to Microsoft, I could not find a copy of Microsoft's specific definition, but the website linked came close enough for this discussion.


Solution:2

Why don't you just create a table do hold the system wide data you need?


Solution:3

You can make a scalar function that returns hard code value. Example:

CREATE FUNCTION [dbo].[APPID]()    RETURNS INT BEGIN         RETURN 1  

To use:

SELECT [dbo].[APPID]()  

or

SELECT * FROM ApplicationAccount WHERE AppID = dbo.APPID()  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »