Stored procedures
-
Upload
randy-riness-south-puget-sound-community-college -
Category
Technology
-
view
233 -
download
1
description
Transcript of Stored procedures
Stored Procedures
CIS-182
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
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
Format of SPROC’s
CREATE PROC <name> <parameter list>
AS<instructions to execute>
EXECUTE
• EXEC(cute) statementOR
• 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)
Uses of Stored Procedures
• For returning data • For editing data• For calculations
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)
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
Sample Input Parameter
CREATE PROC upFindStudent@SID char(9)
ASSELECT *
FROM Students_TWhere SchoolID=@SID
Sample Output Parameter
CREATE PROC upFindStudentID@First varchar(25),@Last varchar(35),@SID char(9) OUTPUT
ASSELECT @SID=SchoolID
FROM Students_T
Where @First=Firstname and @Last=Lastname
Variables
• Create using DECLARE• Need to start with ‘@’• Can use SQL data types or custom data
types
DECLARE @StudentName varchar(50)
Variable Assignment
• SET is usually used similar to procedural languageSET @Var=value
• SELECT is usually used when getting a value from a querySELECT @Var=Sum(PossiblePoints) FROM
Assignments
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
Decision-making
• IF … ELSE– No end if– Need to use Begin/End if have more than one
instruction to executeIF StartDate < EndDate
Begin…End
ELSE
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
Searched CASE
• No test expression• Each WHEN has a boolean test
CASEWHEN Points >= 90 THEN ‘A’WHEN Points < 90 AND Extra > 0
THEN ‘A’
END
@@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
@@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)
@@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