Technical Integration Department System Analysis & Training Group Jerry Yang August 4, 2005 SQL Server 2000 Research Series  Transact-SQL
Introduction Stored Procedure Designing Concepts  Transact-SQL Programming Constructs Basic Knowledge of Function  Summary Agenda
Introduction Stored Procedure Designing Concepts  Transact-SQL Programming Constructs Basic Knowledge of Function Summary Transact-SQL
What Is SQL? Created In Mid-1970s  Used In Commercial Product SQL S tructured   E nglish   QUE ry   L anguage S tructured   Q uery   L anguage What Is ANSI SQL? ANSI SQL-89 ANSI SQL-92 ANSI / ISO SQL-99 ANSI / ISO SQL-2003 Introduction :  IBM :   Oracle (SQL1) (SQL2) (SQL3) (SQL-200n)
What Is Transact-SQL? Sybase & Microsoft SQL Server Extension of SQL Programming Language Introduction
Introduction Stored Procedure Designing Concepts   Transact-SQL Programming Constructs Basic Knowledge of Function Summary Transact-SQL
Definition Stored procedures are batches that SQL Server stores in the database and then executes by name. Why Use Stored Procedure? Performance Issues Centralized Maintenance Data Integrity Consideration Stored Procedure Designing Concepts
Types of Stored Procedures User-defined Stored Procedure System Stored Procedure Trigger View User-defined Function ETC. Stored Procedure Designing Concepts
Types of Stored Procedures User-defined Stored Procedure System Stored Procedure Trigger View User-defined Function ETC. Stored Procedure Designing Concepts
User-defined Stored Procedure Database Selection Stored Procedure Designing Concepts
User-defined Stored Procedure (Continued) Stored Procedure Creation CREATE PROCEDURE Name_List AS  SELECT *  FROM Customer Stored Procedure Deletion DROP PROCEDURE Name_List Limits Maximum Length of Name – 128 Characters Maximum Size of Stored Procedure – 128 MB Stored Procedure Designing Concepts Contents Stored Procedure Name
User-defined Stored Procedure (Continued) Parameters Input Parameters   CREATE PROCEDURE GetName_Multiple @in_Customer_ID INT AS SELECT name FROM  Customer WHERE Customer_ID <= @in_Customer_ID  Example   EXEC GetName_Multiple 10 Stored Procedure Designing Concepts
User-defined Stored Procedure (Continued) Parameters (Continued) Output Parameters   CREATE PROCEDURE GetName_Single @in_Customer_ID INT, @out_Name char(20) OUTPUT AS SELECT @out_Name = name FROM  Customer WHERE Customer_ID = @in_Customer_ID Example Declare @Result_Name varchar(20)   EXEC GetName_Single 125,  @Result_Name  OUTPUT SELECT @Result_Name  Stored Procedure Designing Concepts
Introduction Stored Procedure Designing Concepts  Transact-SQL Programming Constructs Basic Knowledge of Function Summary Transact-SQL
Data Types Four Basic Data Type Categories CHARACTER NUMERIC DATE & TIME MISCELLANEOUS Transact-SQL Programming Constructs
Data Types Four Basic Data Type Categories CHARACTER NUMERIC DATE & TIME MISCELLANEOUS Transact-SQL Programming Constructs
Data Types CHARACTER char Usage:  Declare @Emp_No char(50) varchar Usage :  Declare @Emp_No varchar(50) text Usage :  Declare @Emp_No text Maximum Storage:  2 Giga Bytes Transact-SQL Programming Constructs
Data Types (Continued) CHARACTER (Continued) n char ( N ational char) Usage:  Declare @Emp_No nchar(50) n varchar ( N ational char varying) Usage:  Declare @Emp_No nvarchar(50) n text ( N ational text) Usage:  Declare @Emp_No ntext Maximum Storage:  1 Giga Bytes   Transact-SQL Programming Constructs  Unicode
Data Types (Continued) NUMERIC int Usage:  Declare @Salary int Storage Size:  4 Bytes Range:  -2,147,483,648 (-2G) ~ 2,147,483,647 (2G - 1) smallint Usage:    Declare @Salary smallint Storage Size:  2 Bytes Range:  -32,768 (-32K) ~ 32,767 (32K - 1) tinyint Usage:    Declare @Age tinyint Storage Size:  1 Byte Range:  0 ~ 255 Transact-SQL Programming Constructs
Data Types (Continued) NUMERIC (Continued) bigint Usage:  Declare @Salary bigint Storage Size:  8 Bytes Range:  -9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807 bit Usage:    Declare @Validation bit Storage Size:  1 Bit Range:  0 ~ 1 Decimal, Float, Money, Numeric, ETC. Transact-SQL Programming Constructs
Data Types (Continued) DATE & TIME datetime Usage:  Declare @Emp_Birthday datetime Storage Size:  8 Bytes Example:  2005-08-03 11:10:53 .390 smalldatetime Usage:  Declare @Emp_Birthday smalldatetime Storage Size:  4 Bytes Example:  Jan 1, 2005 12:10:00 Transact-SQL Programming Constructs
Variables Local Variables Declaration @ : Prefix For Local Variables Example:  Declare  @Emp_FirstName  varchar(50) Usage Assigning Value With SELECT Statement Example:  SELECT @Emp_FirstName = ‘Smith’ Assigning Value With SET Statement Example:  SET @Emp_FirstName = ‘Smith’ Transact-SQL Programming Constructs
Variables (Continued) Local Variables (Continued) Usage (Continued) Display The Values of Variables Example:  SELECT @Emp_FirstName   PRINT  @Emp_FirstName Transact-SQL Programming Constructs
Variables (Continued) Global Variables Declaration @@ : Prefix For Global Variables Example:  @@ERROR Example INSERT INTO Employee VALUES(1, ‘John’, ‘Young’ ) ; SELECT @@ERROR Transact-SQL Programming Constructs
Flow-Control Statements Comments Single-Line Comments: -- Example:  --  The following codes were designed by John. Multiple-Line Comments: /*  */ Example:  : /*  The comment starts here! : : The comment ends here!  */ : Transact-SQL Programming Constructs
Flow-Control Statements (Continued) Statement Blocks CREATE PROCEDURE ShowNames AS  BEGIN SELECT Name FROM Employee END Transact-SQL Programming Constructs
Flow-Control Statements (Continued) Conditional Execution – IF Statement CREATE PROCEDURE Order_Handler AS  BEGIN IF @ErrorCode = 0 BEGIN INSERT INTO Order (Order_Date, Goods) VALUES (‘2005/01/01, ‘Camera’) SELECT @Status = @@Error END ELSE : END Transact-SQL Programming Constructs
Flow-Control Statements (Continued) Looping – WHILE Statement CREATE PROCEDURE Order_Handler AS  BEGIN : WHILE @Counter <= 100 BEGIN INSERT INTO Order (Order_ID, Goods) VALUES (@Counter, ‘Camera’) SELECT @Counter = @ Counter + 1 END : END Transact-SQL Programming Constructs
Introduction Stored Procedure Designing Concepts  Transact-SQL Programming Constructs Basic Knowledge of Function Summary Transact-SQL
Types of Functions Build-in Functions Distinguished by The Type of Result Scalar Definition:   Scalar functions refers to the fact that these functions return only one value. Example:   getdate() ,  IsNumeric(‘TEST’) Aggregate Definition:   Aggregate functions perform an operation on a set of records and return a single value. Example:   SELECT Avg(salary) FROM Employee Rowset Definition:   Rowset Functions return a complete recordset to the caller. Example:   OpenQuery(Northwind, ‘select * from orders’)  Basic Knowledge of Function Linked Server Name
Types of Functions (Continued) User-defined Functions (Suspended) Basic Knowledge of Function
Introduction Stored Procedure Designing Concepts  Transact-SQL Programming Constructs Basic Knowledge of Function Summary Transact-SQL
What Do You Need To Know Today… Introduction SQL, ANSI SQL, Transact-SQL Stored Procedure Designing Concepts Stored Procedure Types Stored Procedure Structure Transact-SQL Programming Constructs Data Types – Character, Numeric, Date & Time Variables – Local Variable, Global Variable Flow Control Statements – Block, IF…Then, While … Basic Knowledge of Function Build-in Functions User-defined Functions Summary
Fundamentals of Database Systems Author: Elmasri / Navathe Publisher: Addison-Wesley Publishing Company Inside of Microsoft SQL Server 2000 Author: Kalen Delaney Publisher: Microsoft Press SQL Server 2000 – Stored Procedure & XML Programming Author: Dejan Sunderic Publisher: Brandon A. Nordin Transact-SQL Programming Author: Kevin Kline, Lee Gould, Andrew Zanevsky   Publisher: O’REILLY WWW.DAFFODILDB.COM  Reference
Any Question?

SQL Server 2000 Research Series - Transact SQL

  • 1.
    Technical Integration DepartmentSystem Analysis & Training Group Jerry Yang August 4, 2005 SQL Server 2000 Research Series Transact-SQL
  • 2.
    Introduction Stored ProcedureDesigning Concepts Transact-SQL Programming Constructs Basic Knowledge of Function Summary Agenda
  • 3.
    Introduction Stored ProcedureDesigning Concepts Transact-SQL Programming Constructs Basic Knowledge of Function Summary Transact-SQL
  • 4.
    What Is SQL?Created In Mid-1970s Used In Commercial Product SQL S tructured E nglish QUE ry L anguage S tructured Q uery L anguage What Is ANSI SQL? ANSI SQL-89 ANSI SQL-92 ANSI / ISO SQL-99 ANSI / ISO SQL-2003 Introduction : IBM : Oracle (SQL1) (SQL2) (SQL3) (SQL-200n)
  • 5.
    What Is Transact-SQL?Sybase & Microsoft SQL Server Extension of SQL Programming Language Introduction
  • 6.
    Introduction Stored ProcedureDesigning Concepts Transact-SQL Programming Constructs Basic Knowledge of Function Summary Transact-SQL
  • 7.
    Definition Stored proceduresare batches that SQL Server stores in the database and then executes by name. Why Use Stored Procedure? Performance Issues Centralized Maintenance Data Integrity Consideration Stored Procedure Designing Concepts
  • 8.
    Types of StoredProcedures User-defined Stored Procedure System Stored Procedure Trigger View User-defined Function ETC. Stored Procedure Designing Concepts
  • 9.
    Types of StoredProcedures User-defined Stored Procedure System Stored Procedure Trigger View User-defined Function ETC. Stored Procedure Designing Concepts
  • 10.
    User-defined Stored ProcedureDatabase Selection Stored Procedure Designing Concepts
  • 11.
    User-defined Stored Procedure(Continued) Stored Procedure Creation CREATE PROCEDURE Name_List AS SELECT * FROM Customer Stored Procedure Deletion DROP PROCEDURE Name_List Limits Maximum Length of Name – 128 Characters Maximum Size of Stored Procedure – 128 MB Stored Procedure Designing Concepts Contents Stored Procedure Name
  • 12.
    User-defined Stored Procedure(Continued) Parameters Input Parameters CREATE PROCEDURE GetName_Multiple @in_Customer_ID INT AS SELECT name FROM Customer WHERE Customer_ID <= @in_Customer_ID Example EXEC GetName_Multiple 10 Stored Procedure Designing Concepts
  • 13.
    User-defined Stored Procedure(Continued) Parameters (Continued) Output Parameters CREATE PROCEDURE GetName_Single @in_Customer_ID INT, @out_Name char(20) OUTPUT AS SELECT @out_Name = name FROM Customer WHERE Customer_ID = @in_Customer_ID Example Declare @Result_Name varchar(20) EXEC GetName_Single 125, @Result_Name OUTPUT SELECT @Result_Name Stored Procedure Designing Concepts
  • 14.
    Introduction Stored ProcedureDesigning Concepts Transact-SQL Programming Constructs Basic Knowledge of Function Summary Transact-SQL
  • 15.
    Data Types FourBasic Data Type Categories CHARACTER NUMERIC DATE & TIME MISCELLANEOUS Transact-SQL Programming Constructs
  • 16.
    Data Types FourBasic Data Type Categories CHARACTER NUMERIC DATE & TIME MISCELLANEOUS Transact-SQL Programming Constructs
  • 17.
    Data Types CHARACTERchar Usage: Declare @Emp_No char(50) varchar Usage : Declare @Emp_No varchar(50) text Usage : Declare @Emp_No text Maximum Storage: 2 Giga Bytes Transact-SQL Programming Constructs
  • 18.
    Data Types (Continued)CHARACTER (Continued) n char ( N ational char) Usage: Declare @Emp_No nchar(50) n varchar ( N ational char varying) Usage: Declare @Emp_No nvarchar(50) n text ( N ational text) Usage: Declare @Emp_No ntext Maximum Storage: 1 Giga Bytes Transact-SQL Programming Constructs Unicode
  • 19.
    Data Types (Continued)NUMERIC int Usage: Declare @Salary int Storage Size: 4 Bytes Range: -2,147,483,648 (-2G) ~ 2,147,483,647 (2G - 1) smallint Usage: Declare @Salary smallint Storage Size: 2 Bytes Range: -32,768 (-32K) ~ 32,767 (32K - 1) tinyint Usage: Declare @Age tinyint Storage Size: 1 Byte Range: 0 ~ 255 Transact-SQL Programming Constructs
  • 20.
    Data Types (Continued)NUMERIC (Continued) bigint Usage: Declare @Salary bigint Storage Size: 8 Bytes Range: -9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807 bit Usage: Declare @Validation bit Storage Size: 1 Bit Range: 0 ~ 1 Decimal, Float, Money, Numeric, ETC. Transact-SQL Programming Constructs
  • 21.
    Data Types (Continued)DATE & TIME datetime Usage: Declare @Emp_Birthday datetime Storage Size: 8 Bytes Example: 2005-08-03 11:10:53 .390 smalldatetime Usage: Declare @Emp_Birthday smalldatetime Storage Size: 4 Bytes Example: Jan 1, 2005 12:10:00 Transact-SQL Programming Constructs
  • 22.
    Variables Local VariablesDeclaration @ : Prefix For Local Variables Example: Declare @Emp_FirstName varchar(50) Usage Assigning Value With SELECT Statement Example: SELECT @Emp_FirstName = ‘Smith’ Assigning Value With SET Statement Example: SET @Emp_FirstName = ‘Smith’ Transact-SQL Programming Constructs
  • 23.
    Variables (Continued) LocalVariables (Continued) Usage (Continued) Display The Values of Variables Example: SELECT @Emp_FirstName PRINT @Emp_FirstName Transact-SQL Programming Constructs
  • 24.
    Variables (Continued) GlobalVariables Declaration @@ : Prefix For Global Variables Example: @@ERROR Example INSERT INTO Employee VALUES(1, ‘John’, ‘Young’ ) ; SELECT @@ERROR Transact-SQL Programming Constructs
  • 25.
    Flow-Control Statements CommentsSingle-Line Comments: -- Example: -- The following codes were designed by John. Multiple-Line Comments: /* */ Example: : /* The comment starts here! : : The comment ends here! */ : Transact-SQL Programming Constructs
  • 26.
    Flow-Control Statements (Continued)Statement Blocks CREATE PROCEDURE ShowNames AS BEGIN SELECT Name FROM Employee END Transact-SQL Programming Constructs
  • 27.
    Flow-Control Statements (Continued)Conditional Execution – IF Statement CREATE PROCEDURE Order_Handler AS BEGIN IF @ErrorCode = 0 BEGIN INSERT INTO Order (Order_Date, Goods) VALUES (‘2005/01/01, ‘Camera’) SELECT @Status = @@Error END ELSE : END Transact-SQL Programming Constructs
  • 28.
    Flow-Control Statements (Continued)Looping – WHILE Statement CREATE PROCEDURE Order_Handler AS BEGIN : WHILE @Counter <= 100 BEGIN INSERT INTO Order (Order_ID, Goods) VALUES (@Counter, ‘Camera’) SELECT @Counter = @ Counter + 1 END : END Transact-SQL Programming Constructs
  • 29.
    Introduction Stored ProcedureDesigning Concepts Transact-SQL Programming Constructs Basic Knowledge of Function Summary Transact-SQL
  • 30.
    Types of FunctionsBuild-in Functions Distinguished by The Type of Result Scalar Definition: Scalar functions refers to the fact that these functions return only one value. Example: getdate() , IsNumeric(‘TEST’) Aggregate Definition: Aggregate functions perform an operation on a set of records and return a single value. Example: SELECT Avg(salary) FROM Employee Rowset Definition: Rowset Functions return a complete recordset to the caller. Example: OpenQuery(Northwind, ‘select * from orders’) Basic Knowledge of Function Linked Server Name
  • 31.
    Types of Functions(Continued) User-defined Functions (Suspended) Basic Knowledge of Function
  • 32.
    Introduction Stored ProcedureDesigning Concepts Transact-SQL Programming Constructs Basic Knowledge of Function Summary Transact-SQL
  • 33.
    What Do YouNeed To Know Today… Introduction SQL, ANSI SQL, Transact-SQL Stored Procedure Designing Concepts Stored Procedure Types Stored Procedure Structure Transact-SQL Programming Constructs Data Types – Character, Numeric, Date & Time Variables – Local Variable, Global Variable Flow Control Statements – Block, IF…Then, While … Basic Knowledge of Function Build-in Functions User-defined Functions Summary
  • 34.
    Fundamentals of DatabaseSystems Author: Elmasri / Navathe Publisher: Addison-Wesley Publishing Company Inside of Microsoft SQL Server 2000 Author: Kalen Delaney Publisher: Microsoft Press SQL Server 2000 – Stored Procedure & XML Programming Author: Dejan Sunderic Publisher: Brandon A. Nordin Transact-SQL Programming Author: Kevin Kline, Lee Gould, Andrew Zanevsky Publisher: O’REILLY WWW.DAFFODILDB.COM Reference
  • 35.