tsql: stored procedures: inputs, outputs and everything between

12
Presented by Aaron Buma

Upload: aaron-buma

Post on 27-Jul-2015

427 views

Category:

Software


0 download

TRANSCRIPT

Presented by Aaron Buma

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 ???

Review – Any Questions?

For More InformationVisit http://EmergencyReporting.com to find

out more about Emergency Reporting.

Aaron on the webhttp://AaronBuma.comTwitter: @AaronDBuma