Stored procedures


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Stored procedures

  1. 1. Stored Procedures CIS-182
  2. 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. 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
  4. 4. Format of SPROC’s CREATE PROC <name> <parameter list> AS <instructions to execute>
  5. 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. 6. Uses of Stored Procedures • For returning data • For editing data • For calculations
  7. 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. 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
  9. 9. Sample Input Parameter CREATE PROC upFindStudent @SID char(9) AS SELECT * FROM Students_T Where SchoolID=@SID
  10. 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. 11. Variables • Create using DECLARE • Need to start with ‘@’ • Can use SQL data types or custom data types DECLARE @StudentName varchar(50)
  12. 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. 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. 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. 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. 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. 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. 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. 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