Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.databases.ms-sqlserver > #1135
| NNTP-Posting-Date | Mon, 18 Jun 2012 16:44:07 -0500 |
|---|---|
| From | "Mojo" <please@dont.spam.com> |
| Newsgroups | comp.databases.ms-sqlserver, microsoft.public.sqlserver, microsoft.public.sqlserver.misc, microsoft.public.sqlserver.programming |
| References | <ivGdnW3TprlqH0LSnZ2dnUVZ8kCdnZ2d@brightview.co.uk> <l84vt7tk5g04vau789bc87c2sm1eb1h2b4@4ax.com> |
| Subject | Re: Newbie question: how to use vars in TSQL DDL |
| Date | Mon, 18 Jun 2012 22:43:59 +0100 |
| X-Priority | 3 |
| X-MSMail-Priority | Normal |
| X-Newsreader | Microsoft Outlook Express 6.00.2900.5512 |
| X-MimeOLE | Produced By Microsoft MimeOLE V6.00.2900.5512 |
| Message-ID | <McqdndhTZPa6PELSnZ2dnUVZ8qKdnZ2d@brightview.co.uk> (permalink) |
| Lines | 84 |
| X-Usenet-Provider | http://www.giganews.com |
| X-Trace | sv3-simxk9aFwyWN3rqZHK08TUMtKs1HkasJHkL+vk6L3tmmT+Ff/00ME3EzX1lLl/oWj2pL02yvG3SRABc!8gjY1fYHS+ZiofXGxz5aTy5GDY2ij6T2lWRpk5Z6kCHYszdrc6u4ZapnIg/nIA8dxDHcjTxf3EQ2!Mx96OCrU7jBrEbYYDD9CdQiP |
| X-Abuse-and-DMCA-Info | Please be sure to forward a copy of ALL headers |
| X-Abuse-and-DMCA-Info | Otherwise we will be unable to process your complaint properly |
| X-Postfilter | 1.3.40 |
| X-Original-Bytes | 3824 |
| Path | csiph.com!usenet.pasdenom.info!news.stben.net!border3.nntp.ams.giganews.com!Xl.tags.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!local2.nntp.ams.giganews.com!nntp.brightview.co.uk!news.brightview.co.uk.POSTED!not-for-mail |
| Xref | csiph.com comp.databases.ms-sqlserver:1135 |
Cross-posted to 4 groups.
Show key headers only | View raw
Many thanks everybody.
Much appreciated.
"Gene Wirchenko" <genew@ocis.net> wrote in message
news:l84vt7tk5g04vau789bc87c2sm1eb1h2b4@4ax.com...
On Mon, 18 Jun 2012 20:34:59 +0100, "Mojo" <please@dont.spam.com>
wrote:
>Apols if this a noddy question, but I just can't fathom it!!! :0)
>
>I use a long SQL script (DDL ??) to drop, create and populate my db each
>time (rather than a backup) and initially the DB needs key values inserted
>into it.
I was asking about this recently, too, in
comp.databases.ms-sqlserver and microsoft.public.sqlserver.programming
(two of the four newsgroups you posted to). I wanted to do about the
same as you.
>At the mo, I try to remember to scroll up and down the script (quite long
>now) to populate it with the required values for the given time, but I much
>rather do what I used to do in MySQL, which was to put varaibles at the
>very
>top of my script so that the values entered at the top then reflect further
>down, eg
>
>Line 1 : SET @MyYear= 2012;
>...
>...
>...
>Line 304: .INSERT ... .... ....., @MyYear, ... ....
>
>I've tried this, but it appears as though my GO statements stop it from
>working. I'm probably wrong, but this seems to suggest that I need to set
>the var about 1 or 2 rows above the actual INSERT, which defeats my
>purpose.
A GO statement terminates a batch. A variable's lifetime is that
of the batch that it is declared in.
>Is there a way round this?
I was defining stored procedures which have to be delimited by GO
statements (or BOF/EOF) and following each with its test. Any
variables set at the beginning would be blown away by the first GO,
and I had plenty of them.
You could populate a temp table at the start of your script and
interrogate it later as needed. Temp tables have a lifetime of the
session (if not dropped sooner).
Population:
create table #UnkillableVariables
(
RoughAndToughVariable int
...
);
insert into #UnkillableVariables
(
RoughAndToughVariable int
...
)
values
(
2012
...
);
Interrogation:
declare @RoughAndToughVariable int;
select @RoughAndToughVariable=RoughAndToughVariable
from #UnkillableVariables;
then use it in that batch.
The definition part is lengthy, but the use part is not. I did
not bother, but I might if I revisit this and the script has a long
lifetime.
Sincerely,
Gene Wrichenko
Back to comp.databases.ms-sqlserver | Previous | Next — Previous in thread | Next in thread | Find similar
Newbie question: how to use vars in TSQL DDL "Mojo" <please@dont.spam.com> - 2012-06-18 20:34 +0100
Re: Newbie question: how to use vars in TSQL DDL "Bob Barrows" <reb01501@NOyahooSPAM.com> - 2012-06-18 16:52 -0400
Re: Newbie question: how to use vars in TSQL DDL Gene Wirchenko <genew@ocis.net> - 2012-06-18 13:55 -0700
Re: Newbie question: how to use vars in TSQL DDL "Mojo" <please@dont.spam.com> - 2012-06-18 22:43 +0100
Re: Newbie question: how to use vars in TSQL DDL Erland Sommarskog <esquel@sommarskog.se> - 2012-06-18 23:42 +0200
Re: Newbie question: how to use vars in TSQL DDL rja.carnegie@gmail.com - 2012-06-19 03:50 -0700
Re: Newbie question: how to use vars in TSQL DDL rja.carnegie@gmail.com - 2012-06-19 03:57 -0700
csiph-web