Download presentation
Presentation is loading. Please wait.
Published byDennis Turberville Modified over 9 years ago
1
Stored procedures Procedural programming in Microsoft SQL Server 1Stored procedures
2
No standards Stored procedures are NOT part of the SQL standard Microsoft has a language called T-SQL – Stored procedures aka. ”sproc” Oracle has a language called PL-SQL 2Stored procedures
3
What is … A stored procedure is a [small] program stored in the database Syntax, CREATE CREATE PROCEDURE someName Parameters AS code Example, no parameters CREATE PROCEDURE spBooks AS SELECT * FROM book Syntax, DROP – DROP PROCEDURE someName 3Stored procedures
4
Parameters Stored procedures can have parameters Example CREATE PROCEDURE spBooksAdvanced @countryID int = 11 AS SELECT * FROM book WHERE countryID = @countryID Parameters are named @someName Parameters have a data type – In this case ”int” Parameters might have a default value – In this case 11 4Stored procedures
5
Calling a stored procedure Syntax – EXEC procName parameter(s) Example – EXEC spBooks – EXEC spBooksAdvanced 33 – EXEC spBooksAdvanced Default parameter used A stored procedure might call another stored procedure … 5Stored procedures
6
How to create a simple stored procedure Build up an ordinary SELECT statement – No parameters, set the “wannabe” parameter to a constant value Create the stored procedure – Change the “wannabe” parameter to a real parameter @something Maybe the parameter has a default value – Like NULL 6Stored procedures
7
A little more on default parameters and IF statements Example – CREATE PROCEDURE spBooks – @titleFragment VARCHAR(100) = NULL – AS – If @titleFragment IS NULL SELECT * FROM book – Else SELECT * FROM book WHERE title LIKE ’%’ + @titleFragment + ’%’ Calling – EXEC spBooks // using default parameter NULL – EXEC spBooks sql Stored procedures7
8
INSERT in a stored procedures A stored procedure can also do INSERT CREATE PROCEDURE spInsertTeacher @teachername varchar(100), @salary money AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; INSERT INTO teacher (name, salary) VALUES (@teachername, @salary); END Stored procedures8
9
Throwing exceptions If things go wrong a stored procedure can throw an exception – Raiserror(message, severity, state) Only a single ’e’ Severity: number 1-25. – Higher number means more severe error State: number 1-127 – Place marker: Where did the error take place Stored procedures9
10
Exception example CREATE PROCEDURE spInsertTeacherWithDepartmentnameException @teachername varchar(100), @salary money, @departmentname varchar(50) = null AS BEGIN SET NOCOUNT ON; DECLARE @departmentID INT; SET @departmentID = (select departmentID from department where departmentName = @departmentname); IF (@departmentID is null) BEGIN declare @text varchar(50); set @text = 'No such department ' + cast(@departmentname as varchar); raiserror(@text, 1, 16); END ELSE insert into teacher (Name, salary, departmentID) values (@teachername, @salary, @departmentID); END Stored procedures10
11
Double INSERT example CREATE PROCEDURE spInsertTeacherWithDepartmentnameInsert @teachername varchar(100), @salary money, @departmentname varchar(50) AS BEGIN SET NOCOUNT ON; declare @departmentID int; set @departmentID = (select departmentID from department where departmentName = @departmentname); if (@departmentID is null) begin insert into department (departmentName) values (@departmentname); set @departmentID = (select departmentID from department where departmentName = @departmentname); end insert into teacher (name, salary, departmentID) values (@teachername, @salary, @departmentID); END Stored procedures11
12
What do stored procedures offer? Parameterized procedural action – Parameters means flexibility – Stored procedure is a sequence of statements Security – Users can be granted rights to execute a stored procedure, without having right on the underlying tables Performance – Stored procedures are checked and compiled once At first execution Any later execution will be fast, since no checking and compilation is necessary – Faster than ordinary statements 12Stored procedures
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.