Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dexterity | CONFIDENTIAL 2009 MRO | Analytics | Insights 1 Stored Procedures.

Similar presentations


Presentation on theme: "Dexterity | CONFIDENTIAL 2009 MRO | Analytics | Insights 1 Stored Procedures."— Presentation transcript:

1 Dexterity | CONFIDENTIAL 2009 MRO | Analytics | Insights 1 Stored Procedures

2 Dexterity | CONFIDENTIAL 2009 MRO | Analytics | Insights 2 Stored Procedure  A stored procedure is a precompiled object stored in the database  A stored procedure is a group of Transact-SQL statements compiled into a single execution plan.  Accept input parameters and return multiple values in the form of output parameters to the calling procedure  Contain programming statements that perform operations in the database, including calling other procedures  Return a status value to a calling procedure or batch to indicate success or failure.

3 Dexterity | CONFIDENTIAL 2009 MRO | Analytics | Insights 3 Execution Plan The way that a statement can be physically executed is called an execution plan or a query plan

4 Dexterity | CONFIDENTIAL 2009 MRO | Analytics | Insights 4 Implementing Stored Procedure CREATE PROCEDURE proc_name AS BEGIN sql_statement END EX: CREATE PROCEDURE STORE AS BEGIN SELECT * FROM STUDENT END

5 Dexterity | CONFIDENTIAL 2009 MRO | Analytics | Insights 5 Executing Stored Procedure EXEC/EXECUTE PROCEDURE proc_name EX: EXEC store EXEC "My Stored Procedure" ALTER STORED PROCEDURE EX: ALTER PROCEDURE store AS BEGIN select id, name from student END DROP STORED PROCEDURE DROP PROCEDURE store

6 Dexterity | CONFIDENTIAL 2009 MRO | Analytics | Insights 6 Parameterized Stored Procedure CREATE PROC store @id int, @name varchar(10) AS BEGIN select name from student where id=@id END ------------------------------------------ EXEC store 1, ’balaji’ EXEC store @name=‘balaji’, @id=1 VIEW THE CODE OF STORED PROCEDURE sp_helptext store  create procedure store as begin select * from student end

7 Dexterity | CONFIDENTIAL 2009 MRO | Analytics | Insights 7 Returning Values CREATE PROC store @id int AS BEGIN IF @id>0 RETURN 1 ELSE RETURN 0 END ------------------------------------------ EXEC store 1

8 Dexterity | CONFIDENTIAL 2009 MRO | Analytics | Insights 8 OUTPUT parameter CREATE PROC store @id int, @name varchar(10) OUTPUT, @adrs varchar(10) OUTPUT AS BEGIN IF @id>0 SET @name=‘balaji’ SET @adrs=‘chennai’ RETURN 1 ELSE RETURN 0 END ------------------------------------------ DECLARE @name varchar(10), @adrs varchar(10) EXEC store 1,@name OUTPUT, @adrs OUTPUT PRINT @name PRINT @adrs

9 Dexterity | CONFIDENTIAL 2009 MRO | Analytics | Insights 9 Disadvantage If your data changes a lot then you'd have to recompile a sproc a lot because cached query plans aren't optimal for current data. Stored procedure languages are quite often vendor-specific. If you want to switch to using another vendor's database, then you have to rewrite your stored procedures

10 Dexterity | CONFIDENTIAL 2009 MRO | Analytics | Insights 10 Secured Stored Procedure Embed sp_password/alter login in the code, as a comment Example: CREATE PROCEDURE store WITH ENCRYPTION AS BEGIN 'SQL statements' -- sp_password/alter login END

11 Dexterity | CONFIDENTIAL 2009 MRO | Analytics | Insights 11 SET NOCOUNT Stops the message that shows the count of the number of rows affected by a Transact SQL statement or stored procedure from being returned as part of the result set. These relentless interactions create overhead on the processing and in most cases are not necessary EX CREATE PROCEDURE STORE AS SET NOCOUNT ON BEGIN SELECT * FROM STUDENT END

12 Dexterity | CONFIDENTIAL 2009 MRO | Analytics | Insights 12 Passing table valued parameters in SQL Server 2008 SQL Server 2008 introduces the ability to pass a table data type into stored procedures and functions CREATE TYPE STUDENTTABLETYPE AS TABLE ( ID INT,SNAME VARCHAR(10),ADRS VARCHAR(10) ) DECLARE @tb STUDENTTABLETYPE Insert into @tb values (5, ‘raj, ‘kovai’), (6, ‘ravi, ‘ch’), (7, ‘balu’, ‘madurai’) CREATE PROCEDURE store (@table STUDENTTABLETYPE READONLY ) AS BEGIN insert into student select * from @table where id=5 END Exec store @tb

13 Dexterity | CONFIDENTIAL 2009 MRO | Analytics | Insights 13

14 Dexterity | CONFIDENTIAL 2009 MRO | Analytics | Insights 14 Thank You  Questions ???


Download ppt "Dexterity | CONFIDENTIAL 2009 MRO | Analytics | Insights 1 Stored Procedures."

Similar presentations


Ads by Google