p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

sql_server thread: @variables in views


Message #1 by dont worry <aspmailbox@y...> on Thu, 25 Jan 2001 15:02:02 -0800 (PST)
There really isn't any such thing as a true global variable in SQL
Server.

There is the old thing that SQL Server used to call a global variable
(indeed, you can still look them up under global variables), but those
are actually system functions - you can't create them on the fly.

But what you say. I can create a variable with the @@ sign on the front
and it seems to work, for example:

DECLARE @@MyVar int

SET @@MyVar =3D 5

SELECT @@MyVar


Let's look a little closer though. If it was a true global, it would be
referencable from other object and would last the life of the connection
- not just the batch. But if you run just the SELECT part of the
statement in a separate batch, you'll find that @@MyVar doesn't exist:

DECLARE @@MyVar int

SET @@MyVar =3D 5

SELECT @@MyVar
GO

SELECT @@MyVar


Results in:
           
-----------
5

(1 row(s) affected)

Server: Msg 137, Level 15, State 2, Line 2
Must declare the variable '@@MyVar'.


Everything after the first @ is just considered to be part of the name.
Depending on what you're doing and how desparate you are, I've seen some
people successfully hack together a solution by storing the needed value
in a global temporary table, but that's cheesy at best.

Rob Vieira
Author, Professional SQL Server Programming Series
Check out www.ProfessionalSQL.com! It's new and improved and getting
better by the day!




-----Original Message-----
From: Bukovansky@a... [mailto:Bukovansky@a...]
Sent: Friday, January 26, 2001 12:56 AM
To: SQL Server
Subject: [sql_server] RE: @variables in views


I have the same problem... What you can only is use of global variables
(@@variable)...

Regards,

Richard Bukovansky

> -----Original Message-----
> From: dont worry [mailto:aspmailbox@y...]
> Sent: Friday, January 26, 2001 12:02 AM
> To: SQL Server
> Subject: [sql_server] @variables in views
>
>
> Can you use @variables in views like stored procs? if
> so how?
>
>
> $subst('Email.Unsub')
>



  Return to Index