Create a Global Static Variable in SQL Server

Create a global static variable in SQL Server?

I guess this will work the best for me:

CREATE FUNCTION [dbo].[fn_GetDefaultPercent]()
RETURNS decimal(5,4)
AS
BEGIN
RETURN 1.0000
END

how to declare global variable in SQL Server..?

There is no way to declare a global variable in Transact-SQL. However, if all you want your variables for is to be accessible across batches of a single script, you can use the SQLCMD tool or the SQLCMD mode of SSMS and define that tool/mode-specific variables like this:

:setvar myvar 10

and then use them like this:

$(myvar)

To use SSMS's SQLCMD mode:

Sample Image

how to create a global variable in sql server 2008

I don't know if the SQL Server has global variables, but you can use a user defined function as follows:

CREATE FUNCTION dbo.fn_GetDomainName()
RETURNS STRING
AS
BEGIN
RETURN 'domain_name\\'
END

and do a SELECT dbo.fn_GetDomainName() + Login FROM table WHERE ... at the corresponding locations in your views.

Keep MySQL connection in static variable in function (avoid global variables) in C

First of all - a simple but a bit "crooked" solution might be to only use one function called "db_action" that receives an integer variable that acts as a kind of flag:
When the flag is "0" you try to connect to the DB and when the flag is "1" you disconnect from it:

    MYSQL *db_action(int flag) {

static MYSQL *db_connection = NULL;

if(flag == 0 && db_connection == NULL) {

printf(" NEW CONN \n");

db_connection = (MYSQL *)mysql_init(NULL);

...
}
else if(flag == 1 && db_connection != NULL){
printf("\n closing... \n");

//close connection
mysql_close(db_connection);

//set pointer to null
db_connection = NULL;
}
else{
printf(" OLD CONN \n");
}

return db_connection;
}

This is if you really want to a void using a global variable. In my opinion it makes sense that your connection variable should be global, because it will probably be used for the entire run time of the program and be used by different functions. In many basic mysql tutorial for c/php/ruby etc. they usually use a global variable.

I want you to understand why you cannot set your db_connection variable to NULL in the db_close function.

When you fist declare static MYSQL *db_connection = NULL; in the db_connect function you are creating a new static variable (which means it is initialized only once) and set it to NULL. You can read more here: wikipedia static_variable

Then you exit (your code exits...) the db_connect function. Now the db_connection variable is out of scope - you can't "use" it (read its data or change it). You can only do that inside the db_connect func.

In db_close you get the previous db connection, MYSQL *db_connection = db_connect(); but notice since you declared *db_connection to be a new variable of type MYSQL (it's a pointer to a MYSQL variable) it is now in the scope of the db_close function - and it cannot affect the static variable you created earlier - they are two completely different variables - they only share the same data when you call db_connect().

The scope and/or heap/stack mechanism is something you have to understand when you develop any type of software. Especially C/C++ or similar languages when you as a programmer have more responsibility to manage the memory allocation of the different variables in your programs. Read more about it here: Memory allocation, Heap and Stack

Tom.

Using global variable for SQL select statment - VB.NET

Shared variables are created as soon as the program starts. You have

Public Shared comm2 As SqlCommand = New SqlCommand(GlobalVar.sql2, GlobalVar.conn2)

wich creates the command with sql2 when FileName2 isn't initialized yet.

You could solve this creating a method in your public class to initialize your connections, commands, and DataAdapters...

Public Class GlobalVar

Public Shared Filename2 As String

''build table on left for missing items
Public Shared ds2 As DataSet
Public Shared connStr2 As String
Public Shared sql2 As String
Public Shared conn2 As SqlConnection
Public Shared comm2 As SqlCommand
Public Shared dataadapter2 As SqlDataAdapter

Public Shared Sub InitializeConnection()
''Call this after you fill Filename2
ds2 = New DataSet()
connStr2 = "server=inlt01\SQLEXPRESS; database=DaisyServices; integrated security=yes"
sql2 = "SELECT i.[ID],i.[Site],i.[CLI],i.[CustomerName],i.[FromDate],i.[ToDate],i.[Quantity],i.[UnitCost],i.[TotalCost],i.[Description],i.[filenameonly],i.billingmonth as [CurrentBillingMonth], i.bill From [DaisyServices].[dbo].[DaisyServicesIndigo] i LEFT JOIN [DaisyServices].[dbo].[" & GlobalVar.Filename2 & "] s on i.[SITE]=s.[SITE] AND i.[CLI]=s.[CLI] AND i.[Quantity]=s.[Quantity] AND i.[UnitCost]=s.[UnitCost] AND i.[TotalCost]=s.[TotalCost] AND i. [Description]=s.[Description] WHERE s.[CLI] is NULL"
conn2 = New SqlConnection(GlobalVar.connStr2)
comm2 = New SqlCommand(GlobalVar.sql2, GlobalVar.conn2)
dataadapter2 = New SqlDataAdapter(GlobalVar.comm2)
End Sub
End Class

...BUT I really encourage you to rethink your design. For example, you can create a function in your public class that creates all the connections and adapters you need for that function, open and close those connections and return the DataAdapter you need. Something like:

Public Class GlobalVar
''Do not declare anything here...

Public Shared Function FillData(ByVal Filename2 as string) as DataSet
''...instead put inside this function your connections, data adapters, sql sentences,
''and use them to fill and return the dataset with the data you need
End Function
End Class

Passing a value to a global variable for reference with WinForms (C#) and SQL Connection

Create a User class:

public class User
{
public enum eStatus
{
Client,
Student
}

public static eStatus Status { get; set; }

public static string Name { get; set; }
}

You didn't say what the one variable is, so I guessed it was some kind of identifier like their name.

You can set the User's status with:

User.Status = User.eStatus.Client;

and the name with:

User.Name = "Charlie";

You can access and set the values from anywhere.

Comment response:

The if would look like:

if (Program.User.Status == Program.User.eStatus.Client)
{
// commands
}
else // if (Program.User.Status == Program.User.eStatus.Student)
{
// commands
}

You don't need the second if since there are only two values User.Status can have.

Creating Global Variable For Web Application - ASP.NET

You can use the Application object - it is part of the HttpContext and is directly accessible on any page.

If you don't want to use it, you may want to write a Globals class (or whatever name you like) that holds static members.

public class Globals
{
public static int Counter { get; set;}
}

// accessed from other classes:
Globals.Counter++;

Either approach will not work of you have a web farm or several web applications and will not survive restarts.


Regardless of these options, the right solution (even if you don't want to use it - can you explain why?), is to use the ID field with the IDENTITY clause.

static and globally filter in sql select statement

MySQL supports a local, session and global variables. You want to use session.

To set a session variable, simply:

SET @key = 'value'

All session variables are prefixed with a '@'.

Session variables goes out of scope when the connection is terminated.

In your case, you will still need to AND your queries, like:

SELECT * FROM pages WHERE lang = @lang

You cannot automatically do this.

More info in the documentation.

Using SAS global variable to be called in many sql procs

Once you declare a macro variable using the %let statement, this value is stored in the global table and you do not need to declare it before every proc sql (unless the value for the macro variable x is changed locally in a proc sql step).

When you run '%let x=10000;', the macro variable x is created and will exist during your entire SAS session.

From what I gather from your error, it appears that the global statement '%let x=10000;' was not run at all during the current SAS session.

Also, if you derive a variable within the current proc sql step, you might need to use 'where where calculated trans_amount > &x;' if you apply the subsetting in the same step. (- from what I can see in your amended question, this is not necessary in this case.)

In order for your code to run, you must declare the macro variable x once in your current SAS session.

I think this should solve the problem.



Related Topics



Leave a reply



Submit