Вы находитесь на странице: 1из 13

Module 2: Using Transact-SQL Querying Tools

Overview

n n n n n

SQL Query Analyzer Using the Object Browser Tool in SQL Query Analyzer Using Templates in SQL Query Analyzer Using the osql Utility Executing TransactSQL Statements

SQL Query Analyzer

n n n n n

FreeForm Text Editor ColorCoding of TransactSQL 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:
l l l l l

Scriptobjects Executestoredprocedures Opentables Alterobjectsinthedatabase UseTransactSQLtemplates

Using Templates in SQL Query Analyzer

Templates
l l

Arestartingpointsforcreatingobjectsinadatabase Containparameterstohelpyoucustomizescripts

Format for Template Parameter Definitions <parameter_name,data_type,value>

Using the osql Utility

n n n n

Starting the osql Commandline Utility Using the Interactive Mode Using the Script Execution Mode Using Extended osql Scripting Commands

u Executing TransactSQL Statements

n n n

Dynamically Constructing Statements Using Batches Using Scripts

Dynamically Constructing Statements

n n

Use EXECUTE with String Literals and Variables Use When You Must Assign the Value of the Variable at Execution Time
Example Example 1 1

USE USE library library DECLARE DECLARE @dbname @dbname varchar(30), varchar(30), @tblname @tblname varchar(30) varchar(30) SET SET @dbname @dbname = = 'northwind' 'northwind' SET SET @tblname @tblname = = 'products' 'products' EXECUTE EXECUTE ('USE ('USE ' '+ + @dbname @dbname + +' ' SELECT SELECT * * FROM FROM ' '+ + @tblname) @tblname) GO GO

Using Batches

One or More TransactSQL Statements Submitted Together Defining a Batch with the GO Statement How SQL Server Processes Batches Statements That You Cannot Combine in a Batch
l l l l l

n n n

CREATEPROCEDURE CREATEVIEW CREATETRIGGER CREATERULE CREATEDEFAULT

Using Scripts

A Script Is One or More TransactSQL Statements Saved as a File Using the .sql Extension Scripts:
l l l

Containsavedstatements Canbewrittenusinganytexteditor Canrecreatedatabaseobjectsorexecutestatements repeatedly ExecuteinSQLQueryAnalyzerorinthe osql utility

Recommended Practices

Use Use SQL SQL Query Query Analyzer Analyzer to to Work Work Graphically Graphically and and Interactively Interactively Use Use the the Object Object Browser Browser to to Locate Locate and and Script Script Objects Objects Use Use Templates Templates as as Starting Starting Points Points to to Create Create Objects Objects Use Use the the osql osql Commandline Commandline Utility Utility for for Batch Batch Files Files and and Scheduling Scheduling Save Save Commonly Commonly Used Used TransactSQL TransactSQL Scripts Scripts to to Files Files

Lab A: Creating and Executing TransactSQL Scripts

Review

n n n n n

SQL Server Query Analyzer Using the Object Browser Tool in SQL Query Analyzer Using Templates in SQL Query Analyzer Using the osql Utility Executing TransactSQL Statements

Вам также может понравиться