Introduction To Transact-SQL
Introduction To Transact-SQL
Overview
The Transact-SQL Programming Language Types of Transact-SQL Statements Transact-SQL Syntax Elements
Implements Entry-Level ANSI SQL-92 ISO Standard Can Be Run on Any Entry-Level Compliant Product Contains Additional Unique Functionality
Data Definition Language Statements Data Control Language Statements Data Manipulation Language Statements
CREATE object_name
ALTER object_name DROP object_name
USE northwind CREATE TABLE customer (cust_id int, company varchar(40), contact varchar(30), phone char(12) ) GO
USE DML Statements to Change Data or Retrieve Information SELECT INSERT UPDATE DELETE Must Have the Appropriate Permissions
Batch Directives
GO
EXEC
Comments
In-line Comments
Example 1
Block Comments
Example 3
/* This code retrieves all rows of the products table and displays the unit price, the unit price increased by 10 percent, and the name of the product. */ USE northwind SELECT unitprice, (unitprice * 1.1), productname FROM products GO
Identifiers
Standard Identifiers
First character must be alphabetic Other characters can include letters, numerals, or symbols
Identifiers starting with symbols have special uses Delimited Identifiers Use when names contain embedded spaces Use when reserved words are portions of names Enclose in brackets ([ ]) or quotation marks (" ")
Types of Data
Variables
USE northwind DECLARE @EmpID varchar(11) ,@vlName char(20) SET @vlname = 'Dodsworth' SELECT @EmpID = employeeid FROM employees WHERE LastName = @vlname SELECT @EmpID AS EmployeeID GO
System Functions
Aggregate Functions
Scalar Functions
Rowset Functions
Result Style
ANSI:
Japanese: European:
1998.03.19
1998/03/19 19 Mar 1998 16:34:40:616
Operators
Types of Operators
Expressions
Combination of Symbols and Operators Evaluation to Single Scalar Value Result Data Type Dependent on the Elements Within the Expression
USE SELECT
northwind OrderID, ProductID ,(UnitPrice * Quantity) as ExtendedAmount FROM [Order Details] WHERE (UnitPrice * Quantity) > 10000 GO
Statement Level
Row Level
CASE function
Reserved Keywords
Review
The Transact-SQL Programming Language Types of Transact-SQL Statements Transact-SQL Syntax Elements