To determine which version of Microsoft SQL Server you’re running, run the SQL Server command-line command osql -E -Q "SELECT @@VERSION;"or run the query SELECT SERVERPROPERTY'ProductVer
Trang 1Microsoft SQL Server
Microsoft SQL Server is a commercial
DBMS that supports very large databases
and numbers of transactions It runs on
only Microsoft Windows operating systems
and is complex enough to require a
full-time database administrator (DBA) to run
and maintain it
Learn about SQL Server products at
www.microsoft.com/sqland download a
free 180-day trial copy of SQL Server or a
(permanently) free copy of SQL Server
Express Edition
This book covers Microsoft SQL Server 2008
but also includes tips for 2000 and 2005
To determine which version of Microsoft
SQL Server you’re running, run the SQL
Server command-line command osql -E
-Q "SELECT @@VERSION;"or run the query
SELECT SERVERPROPERTY('ProductVersion');
orSELECT @@VERSION;
✔ Tip
■ You can use the SET ANSI_DEFAULTS ON
option to make SQL Server conform to
standard SQL more closely
10
SQL Server 2000, 2005,
and 2008
If you’re upgrading from SQL Server 2000
to 2005/2008, here are a few things to know about running SQL programs:
◆ SQL Server 2005 and later support some standard SQL features that 2000 doesn’t (such as the EXCEPTand
INTERSECToperators, described in Chapter 9), but most of the SQL examples in this book will run the same in 2000, 2005, and 2008 If an example doesn’t run in all versions, look for a DBMS Tip
◆ SQL Server 2005/2008’s SQL Server Management Studio Query Editor replaces 2000’s SQL Query Analyzer
◆ SQL Server 2005/2008’s sqlcmd
command-line tool replaces 2000’s
osql The sqlcmdtool has many of the same command-line options as
osql(and osqlis still available in 2005/2008 for backward compati-bility) Run sqlcmd -?to show the syntax summary
◆ SQL Server Express Edition is a free, easy-to-use, lightweight version of SQL Server 2005/2008 SQL Server Management Studio Express is a com-panion graphical management tool, available as a separate download or bundled with SQL Server Express Edition
Trang 2SQL Server 2000
To run SQL programs in SQL Server 2000, use the SQL Query Analyzer graphical tool
or the osqlcommand-line tool
To use SQL Query Analyzer:
1. On the Windows desktop, choose Start > All Programs > Microsoft SQL Server >
Query Analyzer
2. In the Connect to SQL Server dialog box, select the server and authentication mode; then click OK
3. On the toolbar (near the top edge of the window), select a database in the
drop-down list (Figure 1.16).
4. To run SQL interactively, type or paste an SQL statement in the query window
or
To run an SQL script, choose File > Open (or press Ctrl+Shift+P); navigate to and select the script file; then click Open
5. Choose Query > Execute (or press F5)
SQL Query Analyzer displays the results
in the bottom pane (Figure 1.17).
■ You also can run isqlwat a command prompt to launch SQL Query Analyzer
Figure 1.16 SQL Query
Analyzer uses the selected database to resolve references in your SQL statements.
Figure 1.17 The results of a SELECT
statement in SQL Query Analyzer.
Trang 3To use the osqlcommand-line tool
interactively:
1. At a command prompt, type:
osql -E -d dbname
The-Eoption tells SQL Server to use a
trusted connection instead of requesting
a password dbname is the name of the
database to use
2. Type an SQL statement The statement
can span multiple lines Terminate it
with a semicolon (;) and then press Enter
3. Type goand then press Enter to display
the results (Figure 1.18).
To use the osqlcommand-line tool in
script mode:
1. At a command prompt, type:
osql -E -d dbname -n -i sql_script
The-Eoption tells SQL Server to use a
trusted connection instead of requesting
a password dbname is the name of the
database to use The -noption suppresses
numbering and prompt symbols (>) in the
output sql_script is a text file containing
SQL statement(s) and can include an
absolute or relative pathname
2 Press Enter to display the results (Figure
1.19).
12
Figure 1.18 The same SELECT statement
in osql interactive mode.
Figure 1.19 The same SELECT statement in osql
script mode.
Trang 4To exit the osqlcommand-line tool:
◆ Type exitorquitand then press Enter
To show osqlcommand-line options:
◆ At a command prompt, type osql -?and then press Enter
✔ Tips
■ If SQL Server makes you specify a user
name and password instead of using a
trusted connection, replace the -Eoption with -U login_id login_id is your user
name.osqlwill prompt you for your
password
■ If SQL Server is running on a remote
network computer, add the option
-S serverto specify the SQL Server
instance to connect to Ask your DBA
for the connection parameters (The -S
option also works for local connections,
when SQL Server is running on your
own PC rather than on some server
elsewhere.)
Trang 5SQL Server 2005/2008
To run SQL programs in SQL Server 2005
and 2008, use the SQL Server Management
Studio graphical tool or the sqlcmd
command-line tool
To use SQL Server Management
Studio:
1. On the Windows desktop, choose Start >
All Programs > Microsoft SQL Server >
SQL Server Management Studio
In SQL Server Express Edition, the
pro-gram is named SQL Server Management
Studio Express
2. In the Connect to Server dialog box,
select the server and authentication
mode; then click Connect
3. In Object Explorer (the left pane),
expand the Databases folder of the server
that you’re using and then select a
data-base (Figure 1.20).
If Object Explorer isn’t visible, choose
View > Object Explorer (or press F8)
4. To run SQL interactively, click
New Query (on the toolbar) or right-click
the database (in Object Explorer) and
choose New Query; then type or paste an
SQL statement in the empty tab that
appears in the right pane
or
To run an SQL script, choose File >
Open > File (or press Ctrl+O); navigate to
and select the script file; then click Open
The file’s contents appear in a new tab in
the right pane
5. Click Execute (on the toolbar) or
choose Query > Execute (or press F5)
SQL Server displays the results in the
bottom pane (Figure 1.21).
14
Figure 1.20 SQL Server Management Studio
uses the selected database to resolve references in your SQL statements.
Figure 1.21 The results of a SELECT statement in SQL Server Management Studio.
Trang 6To use the sqlcmdcommand-line tool interactively:
1. At a command prompt, type:
sqlcmd -d dbname
dbname is the name of the database
to use
2. Type an SQL statement The statement can span multiple lines Terminate it with a semicolon (;) and then press Enter
3. Type goand then press Enter to display
the results (Figure 1.22).
To use the sqlcmdcommand-line tool
in script mode:
1. At a command prompt, type:
sqlcmd -d dbname -i sql_script
dbname is the name of the database to
use sql_script is a text file containing
SQL statement(s) and can include an absolute or relative pathname
2. Press Enter to display the results
(Figure 1.23).
To exit the sqlcmdcommand-line tool:
◆ Type exitorquitand then press Enter
To show sqlcmdcommand-line options:
◆ At a command prompt, type sqlcmd -?
and then press Enter
continues on next page
Figure 1.22 The same SELECT statement in sqlcmd
interactive mode.
Figure 1.23 The same SELECT statement in sqlcmd
script mode.
Trang 7✔ Tips
■ sqlcmdtries to use a trusted connection
by default If instead you have to specify
a user name and password, add the option-U login_id login_id is your user
name.sqlcmdwill prompt you for your password
■ If SQL Server is running on a remote network computer, add the option
-S serverto specify the SQL Server instance to connect to Ask your DBA for the connection parameters (The
-Soption also works for local connec-tions, when SQL Server is running on your own PC rather than on some server elsewhere.)
16
Trang 8Oracle Database is a commercial DBMS that supports very large databases and numbers
of transactions It runs on many operating
systems and hardware platforms and is com-plex enough to require a full-time database
administrator (DBA) to run and maintain it Learn about Oracle products at
www.oracle.comand download Oracle
Express Edition (XE)—a free, starter version
of Oracle Database Documentation is at
www.oracle.com/technology/documentation
This book covers Oracle 11g but also
includes tips for 10g, 9i, and 8i The version
of Oracle that you’re running is displayed in
the initial “Connected to” message that
appears when you log on to SQL*Plus (or run the query SELECT banner FROM v$version;)
To run SQL programs, use the SQL*Plus
(sqlplus) command-line tool
■ To open a command prompt in
Windows, choose Start > All Programs > Accessories > Command Prompt
Trang 9To use the sqlpluscommand-line
tool interactively:
1. At a command prompt, type:
sqlplus user/password@dbname
user is your Oracle user name, password
is your password, and dbname is the
name of the database to connect to For
security, you can omit the password and
instead type:
sqlplus user@dbname
SQL*Plus will prompt you for your
pass-word
2. Type an SQL statement The statement
can span multiple lines Terminate it
with a semicolon (;) and then press Enter
to display the results (Figure 1.24).
To use the sqlpluscommand-line
tool in script mode:
◆ At a command prompt, type:
sqlplus user/password@dbname
➞ @sql_script
user is your Oracle user name, password
is your password, dbname is the name of
the database to connect to, and
sql_script is a text file containing SQL
statement(s) and can include an absolute
or relative pathname For security, you
can omit the password, and instead type:
sqlplus user@dbname @sql_script
SQL*Plus will prompt you for your
pass-word (Figure 1.25).
18
Figure 1.24 The results of a SELECT statement in
sqlplus interactive mode.
Figure 1.25 The same SELECT statement in sqlplus
script mode.
Trang 10To exit the sqlpluscommand-line tool:
◆ Type exitorquitand then press Enter
To show sqlpluscommand-line options:
◆ At a command prompt, type sqlplus -H
and then press Enter
This command displays a few pages that speed by To view one page at a time, type sqlplus -H | moreand then press Enter Tap the spacebar to advance pages
(Figure 1.26).
✔ Tips
■ If you’re running Oracle locally, you can use the user name systemand the pass-word you specified when you created the database:
sqlplus system@dbname
If you’re connecting to a remote Oracle database, ask your DBA for the connec-tion parameters
■ An alternative way to open SQL*Plus in Windows: Choose Start > All Programs > Oracle > Application Development >
SQL Plus
Figure 1.26 The sqlplus help screen.