stored procedures
DESCRIPTION
TRANSCRIPT
![Page 1: Stored procedures](https://reader036.vdocuments.mx/reader036/viewer/2022082700/548c6a2ab479597c0c8b4614/html5/thumbnails/1.jpg)
Stored Procedures
CIS-182
![Page 2: Stored procedures](https://reader036.vdocuments.mx/reader036/viewer/2022082700/548c6a2ab479597c0c8b4614/html5/thumbnails/2.jpg)
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
![Page 3: Stored procedures](https://reader036.vdocuments.mx/reader036/viewer/2022082700/548c6a2ab479597c0c8b4614/html5/thumbnails/3.jpg)
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
![Page 4: Stored procedures](https://reader036.vdocuments.mx/reader036/viewer/2022082700/548c6a2ab479597c0c8b4614/html5/thumbnails/4.jpg)
Format of SPROC’s
CREATE PROC <name> <parameter list>
AS<instructions to execute>
![Page 5: Stored procedures](https://reader036.vdocuments.mx/reader036/viewer/2022082700/548c6a2ab479597c0c8b4614/html5/thumbnails/5.jpg)
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)
![Page 6: Stored procedures](https://reader036.vdocuments.mx/reader036/viewer/2022082700/548c6a2ab479597c0c8b4614/html5/thumbnails/6.jpg)
Uses of Stored Procedures
• For returning data • For editing data• For calculations
![Page 7: Stored procedures](https://reader036.vdocuments.mx/reader036/viewer/2022082700/548c6a2ab479597c0c8b4614/html5/thumbnails/7.jpg)
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)
![Page 8: Stored procedures](https://reader036.vdocuments.mx/reader036/viewer/2022082700/548c6a2ab479597c0c8b4614/html5/thumbnails/8.jpg)
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
![Page 9: Stored procedures](https://reader036.vdocuments.mx/reader036/viewer/2022082700/548c6a2ab479597c0c8b4614/html5/thumbnails/9.jpg)
Sample Input Parameter
CREATE PROC upFindStudent@SID char(9)
ASSELECT *
FROM Students_TWhere SchoolID=@SID
![Page 10: Stored procedures](https://reader036.vdocuments.mx/reader036/viewer/2022082700/548c6a2ab479597c0c8b4614/html5/thumbnails/10.jpg)
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
![Page 11: Stored procedures](https://reader036.vdocuments.mx/reader036/viewer/2022082700/548c6a2ab479597c0c8b4614/html5/thumbnails/11.jpg)
Variables
• Create using DECLARE• Need to start with ‘@’• Can use SQL data types or custom data
types
DECLARE @StudentName varchar(50)
![Page 12: Stored procedures](https://reader036.vdocuments.mx/reader036/viewer/2022082700/548c6a2ab479597c0c8b4614/html5/thumbnails/12.jpg)
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
![Page 13: Stored procedures](https://reader036.vdocuments.mx/reader036/viewer/2022082700/548c6a2ab479597c0c8b4614/html5/thumbnails/13.jpg)
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
![Page 14: Stored procedures](https://reader036.vdocuments.mx/reader036/viewer/2022082700/548c6a2ab479597c0c8b4614/html5/thumbnails/14.jpg)
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
![Page 15: Stored procedures](https://reader036.vdocuments.mx/reader036/viewer/2022082700/548c6a2ab479597c0c8b4614/html5/thumbnails/15.jpg)
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
![Page 16: Stored procedures](https://reader036.vdocuments.mx/reader036/viewer/2022082700/548c6a2ab479597c0c8b4614/html5/thumbnails/16.jpg)
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
![Page 17: Stored procedures](https://reader036.vdocuments.mx/reader036/viewer/2022082700/548c6a2ab479597c0c8b4614/html5/thumbnails/17.jpg)
@@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
![Page 18: Stored procedures](https://reader036.vdocuments.mx/reader036/viewer/2022082700/548c6a2ab479597c0c8b4614/html5/thumbnails/18.jpg)
@@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)
![Page 19: Stored procedures](https://reader036.vdocuments.mx/reader036/viewer/2022082700/548c6a2ab479597c0c8b4614/html5/thumbnails/19.jpg)
@@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