Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.databases.ms-sqlserver > #1134

Re: Newbie question: how to use vars in TSQL DDL

From Erland Sommarskog <esquel@sommarskog.se>
Newsgroups comp.databases.ms-sqlserver, microsoft.public.sqlserver, microsoft.public.sqlserver.misc, microsoft.public.sqlserver.programming
Subject Re: Newbie question: how to use vars in TSQL DDL
Followup-To comp.databases.ms-sqlserver
Date 2012-06-18 23:42 +0200
Organization Erland Sommarskog
Message-ID <XnsA076F13DCEBD1Yazorman@127.0.0.1> (permalink)
References <ivGdnW3TprlqH0LSnZ2dnUVZ8kCdnZ2d@brightview.co.uk>

Cross-posted to 4 groups.

Followups directed to: comp.databases.ms-sqlserver

Show all headers | View raw


Mojo (please@dont.spam.com) writes:
> 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.
> 
> 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, ... ....
 
As long as you are only setting parameters to the script, you can use
SQLCMD. I am not here going to show an example, because I don't use
SQLCMD variables myself. Rather read the topic on SQLCMD in Books
Online.

Keep in mind that these variables are set client-side, so there can be 
no interaction what is going in SQL Server.

-- 
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx

Back to comp.databases.ms-sqlserver | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

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