2. BATCH
⢠Batch is a logical group of SQL statements
â Run-time error will halt execution only of FURTHER
steps
â Can break up work
⢠Stored procedures
⢠User-defined functions
⢠Views
3. Scripts v. Stored Procedures
⢠Script: Text file of SQL commands
⢠Stored Procedure: SQL commands stored in
database itself
â SPROCâs have more capabilities than a script
5. EXECUTE
⢠EXEC(cute) statement
OR
⢠EXEC(cute) stored procedure name
⢠Statement or sproc runs in itâs own scope
â Canât âshareâ variables directly
â Userâs security rules apply
â Canât be used in User Defined Function (UDF)
6. Uses of Stored Procedures
⢠For returning data
⢠For editing data
⢠For calculations
7. Parameters
⢠Method for sending data into and from a stored
procedure
â INPUT parameters are values sent in
â OUTPUT parameters are values returned
⢠Must have a holding space (variable) for the returned data
⢠Defined before start of procedure (AS)
8. Declaring Parameters
⢠Include name and datatype
⢠Default value is optional
â Without a default value, parameter is required
⢠Direction is optional (input is default)
â An output parameter must have direction specified
10. Sample Output Parameter
CREATE PROC upFindStudentID
@First varchar(25),
@Last varchar(35),
@SID char(9) OUTPUT
AS
SELECT @SID=SchoolID
FROM Students_T
Where @First=Firstname and
@Last=Lastname
11. Variables
⢠Create using DECLARE
⢠Need to start with â@â
⢠Can use SQL data types or custom data types
DECLARE @StudentName varchar(50)
12. Variable Assignment
⢠SET is usually used similar to procedural
language
SET @Var=value
⢠SELECT is usually used when getting a value
from a query
SELECT @Var=Sum(PossiblePoints) FROM
Assignments
13. Return Values
⢠Result of stored procedure indicates success or
failure
⢠Non-zero value indicates a problem
⢠Must be an integer
⢠Different from an output parameter
â Output parameter is about data
⢠RETURN <value>
â Causes immediate exit
14. Decision-making
⢠IF ⌠ELSE
â No end if
â Need to use Begin/End if have more than one
instruction to execute
IF StartDate < EndDate
Begin
âŚ
End
ELSE
15. Simple Case Statement
⢠CASE
â Similar to SELECT CASE or SWITCH
â Compares one value to different cases
CASE Category
WHEN âpop_compâ THEN âPopular Computingâ
WHEN âmod_cookâ THEN âModern Cookingâ
END
16. Searched CASE
⢠No test expression
⢠Each WHEN has a boolean test
CASE
WHEN Points >= 90 THEN âAâ
WHEN Points < 90 AND Extra > 0
THEN âAâ
END
17. @@Error â
System Variable
⢠Determined after each SQL statement;
⢠0 means statement was successful
⢠Number other than zero is typically a specific
error
⢠Can store value in variable and test
18. @@Identity â
System Variable
⢠Returns the last identity value used as a result of
INSERT or SELECT INTO
⢠Returns Null if operation failed or a value
wasnât generated
⢠Returns last number created if multiple inserts
occur (i.e. SELECT INTO)
19. @@Rowcount â
System Variables
⢠Number of rows returned or affected by the last
statement
⢠0 (zero) is often used as a logical test
â If no records found for where clause, notify system or
process