Professional Documents
Culture Documents
Slide 1
Objectives
1.
2.
3.
4.
Database Programming
Stored Procedure
Function
Trigger
Slide 2
1. Database Programming
1.1 Variables
1.2 Control-of-Flow Tools
Slide 3
1.1 Variables
Declare a variable:
DECLARE@limitmoney
DECLARE@min_rangeint,@hi_rangeint
Slide 4
WHERE
Slide 5
BEGINEND
IFELSE
CASE WHEN
RETURN [n]
WHILE
PRINT
1.2.1 BEGINEND
Defines a statement block
Other Programming Languages:
C#, Java, C: { }
Pascal, Delphi: BEGIN END
Slide 6
1.2.2 IFELSE
Defines conditional and, optionally, alternate execution
when a condition is false
IFBoolean_expression
T-SQL_statement|block_of_statements
[ELSE
T-SQL_statement|block_of_statements ]
Example:
IF(SELECTytd_salesFROMtitlesWHEREtitle_id='PC1035')>5000
PRINT'Year-to-datesalesare greaterthan$5,000forPC1035.
Slide 7
Slide 9
1.2.5 WHILE
Repeats a statement (or block) while a specific condition is
true
WHILEBoolean_expression
SQL_statement|block_of_statements
[BREAK]SQL_statement|block_of_statements
[CONTINUE]
Example:
WHILE(SELECTAVG(royalty)FROMroysched)<25
BEGIN
UPDATEroyschedSETroyalty=royalty*1.05
IF(SELECTMAX(royalty)FROMroysched)>27 BREAK
ELSE CONTINUE
END
SELECTMAX(royalty)AS"MAXroyalty"
FROMroysched
Slide 10
1.2.6 PRINT
Display message in SQL Query Analyze (Console)
PRINT string
Other Programming Languages:
Java: System.out.print
C#, VB.NET: Console.WriteLine
Slide 11
2. Stored Procedure
2.1 What Is a Stored Procedure?
2.2 Stored Procedure vs. SQL Statement
2.3 Create, update and delete a procedure
Slide 12
Slide 13
Stored Procedure
Creating
- Check syntax
- Compile
First Time
- Check syntax
- Compile
- Execute
- Return data
First Time
- Execute
- Return data
Second Time
- Check syntax
- Compile
- Execute
- Return data
Second Time
- Execute
- Return data
Slide 14
Slide 15
Slide 16
Syntax
Example 1 (Without parameters)
Example 2 (With parameters)
Example 3 (Using RETURN)
2.3.1.1 Syntax
CREATEPROC[EDURE]procedure_name
[@parameter_namedata_type][=default] OUTPUT][,...,n]
AS
T-SQL_statement(s)
Slide 17
Run Procedure
Execute Departments_Members
Slide 18
Run Procedure
Execute Department_Members Accounting
Slide 19
Slide 21
Slide 22
3. Function
3.1
3.2
3.3
3.4
What is a Function?
Scalar functions Example
Inline Table-valued Functions Example
Multi-statement Table-Valued Functions Example
Slide 23
Slide 24
Use:
select dbo.Revenue_In_Day(GETDATE())
Slide 25
Use:
select * from AveragePricebyItems (15.00)
Slide 26
Use:
select * from AveragePricebyItems2 (15.00)
Slide 27
4. Trigger
4.1
4.2
4.3
4.4
4.5
4.6
What is a Trigger?
Creating Syntax
Disable/Enable syntax
Deleted and Inserted tables
Example
Other Functions
Slide 28
Slide 29
Slide 30
Slide 31
Update
new
Inserted Table
new
Delete
old
old
Deleted Table
The values in the inserted and deleted tables are accessible only
within the trigger. Once the trigger is completed, these tables are
no longer accessible.
Slide 32
4.5 Example
CREATE TRIGGER Print_Update
ON Bicycle_Inventory
FOR UPDATE
AS
PRINT "The Bicycle_Inventory table was updated"
Slide 33
Slide 34
Slide 35