Date post: | 27-Jul-2015 |
Category: |
Software |
Upload: | aaron-buma |
View: | 427 times |
Download: | 0 times |
Agenda• What is a Stored Procedure
• Input Parameters and Output Parameters
• How you can call them
• What you can do with them
• How they compare to Scalar and Table-Value functions
What is a Stored Procedure• Encapsulated set of SQL Commands that can be
ran repeatedly
• Execution plan is cached
• It can:• Perform inserts, updates and deletes to data• Execute CRL’s• Contain cursors and loops
Input Parameters• Inputs:• Hardcoded values
• Variables
• Table variables*• *: They must be a custom variable datatype• *: They will be READONLY inside the sproc
Output Parameters• Sprocs can output:• a single value• one or many result sets (using NOCOUNT) • Variables/Parameters
• When calling, the output variable needs to be declared prior to executing the sproc
Methods for calling• EXEC storedProcedureName @Parameters
• EXEC sp_executeSQL @query, @parameterDefinitionString,@parameterA,@parameterB
What you can do in Sprocs• Just about anything• Inserts, Updates and Deletes
• Any deterministic and non-deterministic functions
• Run CLRs (.NET code in the database)
• Execute dynamic SQL
Sprocs vs Scalar Functions• Can’t run non-deterministic functions (ex:
getdate)
• Are treated as a ‘black box’ in the execution plan
• Cannot do INSERTS, UPDATES or DELETEs
• Cannot call CLR’s
• Can only return one result
Vs. Inline Table Value Functions• Cannot do INSERT, UPDATE, DELETE• Cannot run CLRs• Query only, no scripting logic• Returns a result set
• Good for – Having a set query referenced in multiple places AND the plan is optimized
Vs. Multi-Statement Table Value Functions
• Cannot do INSERT, UPDATE, DELETE• Cannot run CLRs• Query plan is not cached – a ‘black box’
• Good for ???