17.06.2013 Views

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 11: Writing Scripts and Batches<br />

If we add a column to our TestTable table in our Test database and then try to reference that column<br />

without ending the first batch:<br />

USE Test;<br />

ALTER TABLE TestTable<br />

ADD col3 int;<br />

INSERT INTO TestTable<br />

(col1, col2, col3)<br />

VALUES<br />

(1,1,1);<br />

we get an error message — <strong>SQL</strong> <strong>Server</strong> cannot resolve the new column name, and therefore complains:<br />

Msg 207, Level 16, State 1, Line 6<br />

Invalid column name ‘col3’.<br />

Add one simple GO statement after the ADD col3 int, however, and everything is working fine:<br />

(1 row(s) affected)<br />

sqlcmd<br />

340<br />

sqlcmd is a utility that allows you to run scripts from a command prompt in a Windows command box.<br />

This can be very nice for executing conversion or maintenance scripts, as well as a quick and dirty way<br />

to capture a text file.<br />

sqlcmd replaces the older osql. osql is still included with <strong>SQL</strong> <strong>Server</strong> for backward compatibility only.<br />

An even older command-line utility — isql — is no longer supported.<br />

The syntax for running sqlcmd from the command line includes a large number of different switches,<br />

and looks like this:<br />

sqlcmd<br />

[ { { -U [ -P ] } | –E } ]<br />

[ -z ] [ -Z and exit]<br />

[-S [ \ ] ] [ -H ] [ -d ]<br />

[ -l ] [ -t ] [ -h ]<br />

[ -s ] [ -w ] [ -a ]<br />

[ -e ] [ -I ]<br />

[ -c ] [ -L [ c ] ] [ -q “” ] [ -Q “” ]<br />

[ -m ] [ -V ] [ -W ] [ -u ] [ -r [ 0 | 1 ] ]<br />

[ -i ] [ -o ]<br />

[ -f | i: [

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!