Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global Variables with GO

Tags:

I'm trying to batch some SQL scripts. At the top I've declared some variables which I deem global in the sense of the term

So:

DECLARE @someVar1 DECLARE @someVar2 ...etc.  GO  Some batch of SQL here that sets @someVar1 and @SomeVar2 to a value and uses it in the SQL statement  GO   Some batch of SQL here that sets @someVar1 and @SomeVar2 to a value and uses it in the SQL statement  GO ... 

the declarations are out of scope...meaning when I run this, the subsequent batch scripts do not find those declarations. Is there a way to keep them global for all batch scripts that are utilizing or setting these variables for use?

like image 305
PositiveGuy Avatar asked Mar 23 '11 19:03

PositiveGuy


People also ask

Does Go have global variables?

A global variable refers to a variable that is defined outside a function. Global variables can be accessed throughout the program or within any function in the defined package. Follow us along as we explore the concept of global variables in the go programming language.

How do you use global variables in Go?

Global variables are defined outside of a function, usually on top of the program. Global variables hold their value throughout the lifetime of the program and they can be accessed inside any of the functions defined for the program.

Is global variable bad in Golang?

Global variables are bad in all programming languages — including Golang.

What is default value of global variable in Go?

What are the default values of local variables and global variables? When we declare local and global variables without assigning any values to them, they are assigned to their default values. In Go, the default values of int and float32 types are 0.


2 Answers

Temporary tables do survive go's:

create table #vars (someVar1 varchar(10), someVar2 int) insert #vars values ('abc',123) 

Get value:

select someVar1 from #vars 

Set value

update #vars set someVar1 = 'def' 

Temporary tables are specific to your connection, so they're not more global than they have to be.

like image 145
Andomar Avatar answered Oct 14 '22 07:10

Andomar


You can declare Scripting Variables, which can be defined explicitly by using the setvar command and don't go out of scope on the GO statement.

e.g.

--Declare the variable :setvar MYDATABASE master --Use the variable USE $(MYDATABASE); SELECT * FROM [dbo].[refresh_indexes] GO --Use again after a GO SELECT * from $(MYDATABASE).[dbo].[refresh_indexes]; GO 

For more info go to: http://technet.microsoft.com/en-us/library/ms188714.aspx

like image 35
Oscar Fraxedas Avatar answered Oct 14 '22 06:10

Oscar Fraxedas