Module 2:
Using Transact-SQL
Querying Tools
Overview
SQL Query Analyzer
Using the Object Browser Tool in SQL
Query Analyzer
Using Templates in SQL Query
Analyzer
Using the osql Utility
Executing Transact-SQL Statements
SQL Query Analyzer
Free-Form Text Editor
Color-Coding of Transact-SQL Syntax
Multiple Query Windows with Grid or
Text Output
Partial Script Execution
Query Execution Information
Using the Object Browser Tool in
SQL Query Analyzer
The Object Browser Enables
Navigation of the Tree View of Objects
in a Database
Using the Object Browser, You Can:
Script objects
Execute stored procedures
Open tables
Alter objects in the database
Use Transact-SQL templates
Using Templates in SQL Query
Analyzer
Templates
Are starting points for creating objects in
a database
Contain parameters to help you
customize scripts
Format for Template Parameter
Definitions
<parameter_name, data_type, value>
Using the osql Utility
Starting the osql Command-line Utility
Using the Interactive Mode
Using the Script Execution Mode
Using Extended osql Scripting
Commands
Executing Transact-SQL
Statements
Dynamically Constructing Statements
Using Batches
Using Scripts
Dynamically Constructing
Statements
Use EXECUTE with String Literals and
Variables
Use When You Must Assign the Value
of the Variable
at Execution Time Example 1
USE library
DECLARE @dbname varchar(30), @tblname varchar(30)
SET @dbname = 'northwind'
SET @tblname = 'products'
EXECUTE
('USE ' + @dbname + ' SELECT * FROM ' + @tblname)
GO
Using Batches
One or More Transact-SQL Statements
Submitted Together
Defining a Batch with the GO
Statement
How SQL Server Processes Batches
Statements That You Cannot Combine
in a Batch
CREATE PROCEDURE
CREATE VIEW
CREATE TRIGGER
CREATE RULE
CREATE DEFAULT
Using Scripts
A Script Is One or More Transact-SQL
Statements Saved as a File Using the
.sql Extension
Scripts:
Contain saved statements
Can be written using any text editor
Can recreate database objects or
execute statements repeatedly
Execute in SQL Query Analyzer or in the
osql utility
Recommended Practices
Use SQL Query Analyzer to Work Graphically and Interact
Use the Object Browser to Locate and Script Objects
Use Templates as Starting Points to Create Objects
Use the osql Command-line Utility for Batch Files and Sche
Save Commonly Used Transact-SQL Scripts to Files
Lab A: Creating and Executing
Transact-SQL Scripts
Review
SQL Server Query Analyzer
Using the Object Browser Tool in SQL
Query Analyzer
Using Templates in SQL Query
Analyzer
Using the osql Utility
Executing Transact-SQL Statements