Presentation is loading. Please wait.

Presentation is loading. Please wait.

Stored procedures Procedural programming in Microsoft SQL Server 1Stored procedures.

Similar presentations


Presentation on theme: "Stored procedures Procedural programming in Microsoft SQL Server 1Stored procedures."— Presentation transcript:

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 int = 11 AS SELECT * FROM book WHERE countryID Parameters are 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 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 VARCHAR(100) = NULL – AS – IS NULL SELECT * FROM book – Else SELECT * FROM book WHERE title LIKE ’%’ + ’%’ 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 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) 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 – Higher number means more severe error State: number – Place marker: Where did the error take place Stored procedures9

10 Exception example CREATE PROCEDURE varchar(50) = null AS BEGIN SET NOCOUNT ON; INT; = (select departmentID from department where departmentName IF is null) BEGIN varchar(50); = 'No such department ' + as varchar); 1, 16); END ELSE insert into teacher (Name, salary, END Stored procedures10

11 Double INSERT example CREATE PROCEDURE varchar(50) AS BEGIN SET NOCOUNT ON; int; = (select departmentID from department where departmentName if is null) begin insert into department (departmentName) values = (select departmentID from department where departmentName end insert into teacher (name, salary, 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


Download ppt "Stored procedures Procedural programming in Microsoft SQL Server 1Stored procedures."

Similar presentations


Ads by Google