SlideShare ist ein Scribd-Unternehmen logo
1 von 19
Stored Procedures
CIS-182
BATCH
• Batch is a logical group of SQL statements
– Run-time error will halt execution only of FURTHER
steps
– Can break up work
• Stored procedures
• User-defined functions
• Views
Scripts v. Stored Procedures
• Script: Text file of SQL commands
• Stored Procedure: SQL commands stored in
database itself
– SPROC’s have more capabilities than a script
Format of SPROC’s
CREATE PROC <name>
<parameter list>
AS
<instructions to execute>
EXECUTE
• EXEC(cute) statement
OR
• EXEC(cute) stored procedure name
• Statement or sproc runs in it’s own scope
– Can’t ‘share’ variables directly
– User’s security rules apply
– Can’t be used in User Defined Function (UDF)
Uses of Stored Procedures
• For returning data
• For editing data
• For calculations
Parameters
• Method for sending data into and from a stored
procedure
– INPUT parameters are values sent in
– OUTPUT parameters are values returned
• Must have a holding space (variable) for the returned data
• Defined before start of procedure (AS)
Declaring Parameters
• Include name and datatype
• Default value is optional
– Without a default value, parameter is required
• Direction is optional (input is default)
– An output parameter must have direction specified
Sample Input Parameter
CREATE PROC upFindStudent
@SID char(9)
AS
SELECT *
FROM Students_T
Where SchoolID=@SID
Sample Output Parameter
CREATE PROC upFindStudentID
@First varchar(25),
@Last varchar(35),
@SID char(9) OUTPUT
AS
SELECT @SID=SchoolID
FROM Students_T
Where @First=Firstname and
@Last=Lastname
Variables
• Create using DECLARE
• Need to start with ‘@’
• Can use SQL data types or custom data types
DECLARE @StudentName varchar(50)
Variable Assignment
• SET is usually used similar to procedural
language
SET @Var=value
• SELECT is usually used when getting a value
from a query
SELECT @Var=Sum(PossiblePoints) FROM
Assignments
Return Values
• Result of stored procedure indicates success or
failure
• Non-zero value indicates a problem
• Must be an integer
• Different from an output parameter
– Output parameter is about data
• RETURN <value>
– Causes immediate exit
Decision-making
• IF … ELSE
– No end if
– Need to use Begin/End if have more than one
instruction to execute
IF StartDate < EndDate
Begin
…
End
ELSE
Simple Case Statement
• CASE
– Similar to SELECT CASE or SWITCH
– Compares one value to different cases
CASE Category
WHEN ‘pop_comp’ THEN ‘Popular Computing’
WHEN ‘mod_cook’ THEN ‘Modern Cooking’
END
Searched CASE
• No test expression
• Each WHEN has a boolean test
CASE
WHEN Points >= 90 THEN ‘A’
WHEN Points < 90 AND Extra > 0
THEN ‘A’
END
@@Error –
System Variable
• Determined after each SQL statement;
• 0 means statement was successful
• Number other than zero is typically a specific
error
• Can store value in variable and test
@@Identity –
System Variable
• Returns the last identity value used as a result of
INSERT or SELECT INTO
• Returns Null if operation failed or a value
wasn’t generated
• Returns last number created if multiple inserts
occur (i.e. SELECT INTO)
@@Rowcount –
System Variables
• Number of rows returned or affected by the last
statement
• 0 (zero) is often used as a logical test
– If no records found for where clause, notify system or
process

Weitere ähnliche Inhalte

Was ist angesagt?

10g plsql slide
10g plsql slide10g plsql slide
10g plsql slide
Tanu_Manu
 
Do IT with SQL
Do IT with SQLDo IT with SQL
Do IT with SQL
Nur Hidayat
 

Was ist angesagt? (19)

XML
XMLXML
XML
 
Mule data weave_4
Mule data weave_4Mule data weave_4
Mule data weave_4
 
Reactjs workshop
Reactjs workshopReactjs workshop
Reactjs workshop
 
CIS 282 Final Review
CIS 282 Final ReviewCIS 282 Final Review
CIS 282 Final Review
 
SQL Intro
SQL IntroSQL Intro
SQL Intro
 
Oracle PL/SQL Best Practices
Oracle PL/SQL Best PracticesOracle PL/SQL Best Practices
Oracle PL/SQL Best Practices
 
Analytical Functions for DWH
Analytical Functions for DWHAnalytical Functions for DWH
Analytical Functions for DWH
 
Presto Functions
Presto FunctionsPresto Functions
Presto Functions
 
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 6...
 Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 6... Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 6...
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 6...
 
High Performance Plsql
High Performance PlsqlHigh Performance Plsql
High Performance Plsql
 
iOS Beginners Lesson 1
iOS Beginners Lesson 1iOS Beginners Lesson 1
iOS Beginners Lesson 1
 
Jumping Into Java Then!
Jumping Into Java Then!Jumping Into Java Then!
Jumping Into Java Then!
 
Web Service Workshop - 3 days
Web Service Workshop - 3 daysWeb Service Workshop - 3 days
Web Service Workshop - 3 days
 
Coding standards
Coding standards Coding standards
Coding standards
 
C# advanced topics and future - C#5
C# advanced topics and future - C#5C# advanced topics and future - C#5
C# advanced topics and future - C#5
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQL
 
Dynamic Publishing with Arbortext Data Merge
Dynamic Publishing with Arbortext Data MergeDynamic Publishing with Arbortext Data Merge
Dynamic Publishing with Arbortext Data Merge
 
10g plsql slide
10g plsql slide10g plsql slide
10g plsql slide
 
Do IT with SQL
Do IT with SQLDo IT with SQL
Do IT with SQL
 

Ähnlich wie Stored procedures

Intro to tsql unit 14
Intro to tsql   unit 14Intro to tsql   unit 14
Intro to tsql unit 14
Syed Asrarali
 
Agile db testing_techniques
Agile db testing_techniquesAgile db testing_techniques
Agile db testing_techniques
Tarik Essawi
 
Empower my sql server administration with 5.7 instruments
Empower my sql server administration with 5.7 instrumentsEmpower my sql server administration with 5.7 instruments
Empower my sql server administration with 5.7 instruments
Marco Tusa
 

Ähnlich wie Stored procedures (20)

Intro to tsql unit 14
Intro to tsql   unit 14Intro to tsql   unit 14
Intro to tsql unit 14
 
Intro to tsql
Intro to tsqlIntro to tsql
Intro to tsql
 
5. stored procedure and functions
5. stored procedure and functions5. stored procedure and functions
5. stored procedure and functions
 
Stored procedures
Stored proceduresStored procedures
Stored procedures
 
Stored procedures
Stored proceduresStored procedures
Stored procedures
 
Store programs
Store programsStore programs
Store programs
 
Agile db testing_techniques
Agile db testing_techniquesAgile db testing_techniques
Agile db testing_techniques
 
Empower my sql server administration with 5.7 instruments
Empower my sql server administration with 5.7 instrumentsEmpower my sql server administration with 5.7 instruments
Empower my sql server administration with 5.7 instruments
 
New T-SQL Features in SQL Server 2012
New T-SQL Features in SQL Server 2012 New T-SQL Features in SQL Server 2012
New T-SQL Features in SQL Server 2012
 
Informatica overview
Informatica overviewInformatica overview
Informatica overview
 
Informatica overview
Informatica overviewInformatica overview
Informatica overview
 
SQL Server Stored procedures
SQL Server Stored proceduresSQL Server Stored procedures
SQL Server Stored procedures
 
An Approach to Sql tuning - Part 1
An Approach to Sql tuning - Part 1An Approach to Sql tuning - Part 1
An Approach to Sql tuning - Part 1
 
Pl sql
Pl sqlPl sql
Pl sql
 
PL/SQL Tips and Techniques Webinar Presentation
PL/SQL Tips and Techniques Webinar PresentationPL/SQL Tips and Techniques Webinar Presentation
PL/SQL Tips and Techniques Webinar Presentation
 
Procedures/functions of rdbms
Procedures/functions of rdbmsProcedures/functions of rdbms
Procedures/functions of rdbms
 
Performance Schema for MySQL Troubleshooting
Performance Schema for MySQL TroubleshootingPerformance Schema for MySQL Troubleshooting
Performance Schema for MySQL Troubleshooting
 
Dreamforce Campfire - Apex Testing Tips and Tricks
Dreamforce Campfire - Apex Testing Tips and TricksDreamforce Campfire - Apex Testing Tips and Tricks
Dreamforce Campfire - Apex Testing Tips and Tricks
 
Performance Tuning with Execution Plans
Performance Tuning with Execution PlansPerformance Tuning with Execution Plans
Performance Tuning with Execution Plans
 
PL/SQL Fundamentals I
PL/SQL Fundamentals IPL/SQL Fundamentals I
PL/SQL Fundamentals I
 

Mehr von Randy Riness @ South Puget Sound Community College

Mehr von Randy Riness @ South Puget Sound Community College (20)

Normalization
NormalizationNormalization
Normalization
 
CIS160 final review
CIS160 final reviewCIS160 final review
CIS160 final review
 
SQL Constraints
SQL ConstraintsSQL Constraints
SQL Constraints
 
CIS 245 Final Review
CIS 245 Final ReviewCIS 245 Final Review
CIS 245 Final Review
 
CIS145 Final Review
CIS145 Final ReviewCIS145 Final Review
CIS145 Final Review
 
Cis166 Final Review C#
Cis166 Final Review C#Cis166 Final Review C#
Cis166 Final Review C#
 
Classes and Objects
Classes and ObjectsClasses and Objects
Classes and Objects
 
CIS245 sql
CIS245 sqlCIS245 sql
CIS245 sql
 
Cis245 Midterm Review
Cis245 Midterm ReviewCis245 Midterm Review
Cis245 Midterm Review
 
CSS
CSSCSS
CSS
 
XPath
XPathXPath
XPath
 
XSLT Overview
XSLT OverviewXSLT Overview
XSLT Overview
 
Views
ViewsViews
Views
 
CIS282 Midterm review
CIS282 Midterm reviewCIS282 Midterm review
CIS282 Midterm review
 
Schemas 2 - Restricting Values
Schemas 2 - Restricting ValuesSchemas 2 - Restricting Values
Schemas 2 - Restricting Values
 
CIS 145 test 1 review
CIS 145 test 1 reviewCIS 145 test 1 review
CIS 145 test 1 review
 
XML schemas
XML schemasXML schemas
XML schemas
 
Document type definitions part 2
Document type definitions part 2Document type definitions part 2
Document type definitions part 2
 
Document type definitions part 1
Document type definitions part 1Document type definitions part 1
Document type definitions part 1
 
DOM specifics
DOM specificsDOM specifics
DOM specifics
 

KĂźrzlich hochgeladen

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 

KĂźrzlich hochgeladen (20)

Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 

Stored procedures

  • 2. BATCH • Batch is a logical group of SQL statements – Run-time error will halt execution only of FURTHER steps – Can break up work • Stored procedures • User-defined functions • Views
  • 3. Scripts v. Stored Procedures • Script: Text file of SQL commands • Stored Procedure: SQL commands stored in database itself – SPROC’s have more capabilities than a script
  • 4. Format of SPROC’s CREATE PROC <name> <parameter list> AS <instructions to execute>
  • 5. EXECUTE • EXEC(cute) statement OR • EXEC(cute) stored procedure name • Statement or sproc runs in it’s own scope – Can’t ‘share’ variables directly – User’s security rules apply – Can’t be used in User Defined Function (UDF)
  • 6. Uses of Stored Procedures • For returning data • For editing data • For calculations
  • 7. Parameters • Method for sending data into and from a stored procedure – INPUT parameters are values sent in – OUTPUT parameters are values returned • Must have a holding space (variable) for the returned data • Defined before start of procedure (AS)
  • 8. Declaring Parameters • Include name and datatype • Default value is optional – Without a default value, parameter is required • Direction is optional (input is default) – An output parameter must have direction specified
  • 9. Sample Input Parameter CREATE PROC upFindStudent @SID char(9) AS SELECT * FROM Students_T Where SchoolID=@SID
  • 10. Sample Output Parameter CREATE PROC upFindStudentID @First varchar(25), @Last varchar(35), @SID char(9) OUTPUT AS SELECT @SID=SchoolID FROM Students_T Where @First=Firstname and @Last=Lastname
  • 11. Variables • Create using DECLARE • Need to start with ‘@’ • Can use SQL data types or custom data types DECLARE @StudentName varchar(50)
  • 12. Variable Assignment • SET is usually used similar to procedural language SET @Var=value • SELECT is usually used when getting a value from a query SELECT @Var=Sum(PossiblePoints) FROM Assignments
  • 13. Return Values • Result of stored procedure indicates success or failure • Non-zero value indicates a problem • Must be an integer • Different from an output parameter – Output parameter is about data • RETURN <value> – Causes immediate exit
  • 14. Decision-making • IF … ELSE – No end if – Need to use Begin/End if have more than one instruction to execute IF StartDate < EndDate Begin … End ELSE
  • 15. Simple Case Statement • CASE – Similar to SELECT CASE or SWITCH – Compares one value to different cases CASE Category WHEN ‘pop_comp’ THEN ‘Popular Computing’ WHEN ‘mod_cook’ THEN ‘Modern Cooking’ END
  • 16. Searched CASE • No test expression • Each WHEN has a boolean test CASE WHEN Points >= 90 THEN ‘A’ WHEN Points < 90 AND Extra > 0 THEN ‘A’ END
  • 17. @@Error – System Variable • Determined after each SQL statement; • 0 means statement was successful • Number other than zero is typically a specific error • Can store value in variable and test
  • 18. @@Identity – System Variable • Returns the last identity value used as a result of INSERT or SELECT INTO • Returns Null if operation failed or a value wasn’t generated • Returns last number created if multiple inserts occur (i.e. SELECT INTO)
  • 19. @@Rowcount – System Variables • Number of rows returned or affected by the last statement • 0 (zero) is often used as a logical test – If no records found for where clause, notify system or process