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

Module 2: Overview of

Programming SQL
Server
Vidya Vrat Agarwal. | MCT, MCSD
SQL Server Programming Tools

 SQL Query Analyzer


 Color-codes syntax elements automatically
 Multiple query windows
 Customizable views of result sets
 Graphical execution plans
 Execute portions of scripts
 osql Utility
 Command-line utility
The Transact-SQL Programming Language

 SQL Server Implementation of Entry-Level ANSI ISO


Standard
 Can Be Run on Any Entry-Level Compliant Product
 Contains Additional Unique Functionality
Elements of Transact-SQL

 Data Control Language Statements


 Data Definition Language Statements
 Data Manipulation Language Statements
Data Control Language Statements

 Set or Change Permissions


 GRANT
 DENY
 REVOKE
 By Default, Only sysadmin, dbcreator, db_owner, and
db_securityadmin Roles Can Execute
Data Definition Language Statements

 Define the Database Objects


 CREATE object_type object_name
 ALTER object_type object_name
 DROP object_type object_name
Data Manipulation Language Statements

 Use When Working with Data in the Database


 SELECT
 INSERT
 UPDATE
 DELETE
Naming Guidelines

 Use Meaningful Names Where Possible


 Keep Names Short
 Use a Clear and Simple Naming Convention
 Chose an Identifier That Distinguishes Types of Objects
 Views
 Stored procedures
 Keep Object Names and User Names Unique
Standard Identifiers
Standard identifiers can contain from one to 128 characters, including
letters, symbols (_, @, or #), and numbers. No embedded spaces are
allowed in standard identifiers.
Rules for using identifiers include:
. The first character must be an alphabetic character of a-z or A-Z.
. After the first character, identifiers can include letters, numerals, or
the @, $, #, or _.
Identifier names starting with a symbol have special uses:
 An identifier beginning with the @ symbol denotes a local variable
or parameter.
 An identifier beginning with a pound sign (#) denotes a temporary
table or procedure. An identifier beginning with a double pound sign
(##) denotes a global temporary object.
Delimited Identifiers
Delimited identifiers can be used in the following situations:
. When names contain embedded spaces
. When reserved words are used for object names or portions of object
names
Delimited identifiers must be enclosed in brackets or double quotation
marks when they are used in Transact-SQL statements.
. Bracketed identifiers are delimited by square brackets ([ ]):

Quoted identifiers are delimited by double quotation marks (""):


Additional Language Elements

 Local Variables
 Operators
 Functions
 Function Examples
 Control of Flow Language Elements
 Comments
Local Variables
 User-defined with DECLARE Statement
 Assigned Values with SET or Select Statement
DECLARE
DECLARE @vLastName
@vLastName char(20),
char(20),
@vFirstName
@vFirstName varchar(11)
varchar(11)

SET
SET @vLastName
@vLastName == 'Dodsworth'
'Dodsworth'

SELECT
SELECT @vFirstName
@vFirstName == FirstName
FirstName
FROM
FROM Northwind..Employees
Northwind..Employees
WHERE
WHERE LastName
LastName == @vLastName
@vLastName
PRINT
PRINT @vFirstName
@vFirstName ++ '' '' ++ @vLastName
@vLastName
Operators

 Types of Operators
 Arithmetic
 Comparison
 String concatenation
 Logical
 Operator Precedence Levels
Comments
 In-Line Comments

SELECT
SELECT ProductName,
ProductName,
(UnitsInStock
(UnitsInStock ++ UnitsOnOrder)
UnitsOnOrder) AS
AS Max
Max --
-- Calculates
Calculates inventory
inventory
,, SupplierID
SupplierID
FROM
FROM Products
Products

 Block Comments
/*
/*
**
** This
This code
code retrieves
retrieves all
all rows
rows of
of the
the products
products table
table
** and displays the unit price, the unit price
** and displays the unit price, the unit price increasedincreased
**
** by
by 10
10 percent,
percent, and
and the
the name
name of
of the
the product.
product.
*/
*/
SELECT
SELECT UnitPrice,
UnitPrice, (UnitPrice
(UnitPrice ** 1.1),
1.1), ProductName
ProductName
FROM Products
FROM Products
Ways to Execute Transact-SQL Statements

 Dynamically Constructing Statements


 Using Batches
 Using Scripts
 Using Transactions
 Using XML
Dynamically Constructing Statements

 Use EXECUTE with String Literals and Variables


 Use When You Must Assign Value of Variable at
Execution Time
 Any Variables and Temporary Tables Last Only
During Execution

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)
Using Batches

 One or More Transact-SQL Statements


Submitted Together
 Define a Batch by Using the GO Statement
 How SQL Server Processes Batches
 You Cannot Combine Some Statements in a Batch
 CREATE PROCEDURE
 CREATE VIEW
 CREATE TRIGGER
 CREATE RULE
 CREATE DEFAULT
Using Scripts

 Contain Saved Statements


 Can Be Written in Any Text Editor
 Save by using .sql file name extension
 Execute in SQL Query Analyzer or osql Utility
 Use to Recreate Database Objects or to Execute
Statements Repeatedly
Using Transactions

 Processed Like a Batch


 Data Integrity Is Guaranteed
 Changes to the Database Are Either Applied Together or
Rolled Back

BEGIN
BEGIN TRANSACTION
TRANSACTION
UPDATE
UPDATE savings
savings SET
SET amount
amount == (amount
(amount -- 100)
100)
WHERE
WHERE custid
custid == 78910
78910
…… <Rollback
<Rollback transaction
transaction if
if error>
error>
UPDATE
UPDATE checking
checking SET
SET amount
amount == (amount
(amount ++ 100)
100)
WHERE
WHERE custid
custid == 78910
78910
…… <Rollback
<Rollback transaction
transaction if
if error>
error>
COMMIT
COMMIT TRANSACTION
TRANSACTION
Check Your Understanding.
Q.1. What are the features of SQL Server Query
Analyzer.?
Q.2. What are the elements of T-SQL .?
Q.3. Which SQL Commands are meant for Data
Control language.?
Q.4. Which SQL Commands meant for Data Definition
Language.?
Q.5. Which SQL Commands meant for Data
Manipulation Language.?
Q.6. What is delimited identifier.?
Q.7. What are the types of Comments available in
SQL Server.?
Q.8. What is the Similarity between a Batch and a
Script.?
Q.9. What is the Difference between a Batch and a
Script.?
Thank You.

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