Escolar Documentos
Profissional Documentos
Cultura Documentos
Programming SQL
Server
Vidya Vrat Agarwal. | MCT, MCSD
SQL Server Programming Tools
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
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
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.