SQL Server 2000 Research Series - Transact SQL

1,261 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,261
On SlideShare
0
From Embeds
0
Number of Embeds
734
Actions
Shares
0
Downloads
14
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

SQL Server 2000 Research Series - Transact SQL

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

×