SlideShare a Scribd company logo
2
Most read
7
Most read
17
Most read
12SQL SERVER: SQLFUNCTIONS AND PROCEDURES
SQL ProceduresWhat is an SQL Procedure?	An SQL Procedure contains a  group of sql statements which solve a common purpose. Syntax:Consider a simple SQL Procedure:Create proc printProcedureAsPrint ‘Hello World’goCreate proc <procedureName>As…. Statements…Print is a command in SQL Server 2008. It is used to print a string on the screen.NOTE: The SQL Procedures that we learn here are stored in the system by the DBMS. They are hence known as stored procedures
Altering stored proceduresHow to alter a SQL stored procedure?	A Stored procedure can be altered using the alter proc command.Syntax:Consider a simple SQL Procedure:Alter proc printProcedureAsPrint ‘New Hello World’goAlter proc<procedureName>As…. New Statements…
Executing ProceduresThe Main advantage of using a stored procedure is its reusability, i.e., a procedure can be called any time that it needs to be executed.An SQL procedure can be executed using the exec command:Exec <ProcedureName>Example: Create proc printProcedureAsPrint ‘New Hello World’GoExec printProcedureOUTPUT:New Hello World
Procedure ParametersA Procedure, like a C or C++ Procedure(function) can take up input parameters and produce return values.Output Parameters (Returned Values)SQL ProcedureInput ParametersA Procedure with Input Parameters:Create proc hellop@info1 int= 1,@info2 intAsPrint @info1+@info2Calling a Procedure with parameters:Exec proc hellop 3,2
Procedure ParametersCalling a Procedure with parameters:Exec proc hellop 3,2Here, the value ‘3’ over-rides the default value. If a paramater does not have a default value defined for it, then the DBMS requires the user to pass a value for it before execution.Eg: exec proc hellop 1: will return an error as the second parameter is not assigned any value.
Procedure ParametersOutput Parameters:A Procedure can have output parameters also. The Output parameters are specified by the keyword output.Consider a procedure to add two numbers:Create proc adder@num1 int =0,@num2 int =0,AsDeclare @num3 int= @num1 + @num2;Print @num3;GoExecuting the procedure: exec proc adder 3,4  Output: 7A Procedure can also return a value as: return <value>NOTEThe Data members in SQL (@num1) resemble the variables in c/c++. But there is a big difference: They can’t be altered directly.Eg: @num1 = @num1 +1 Is invalid. The values are assigned at the time of their declaration only.
Procedure ParametersUsing the Output parameters:CREATE PROC Sales  @CustomerIDint,  @AccNumvarchar(10) OUTPUTASSELECT @AccNum = AccNum FROM Sales.Customer WHERE CustomerID = @CustomerID;GOCalling a procedure with return values: DECLARE @AccNumvarchar(10);EXEC GetCustomerAccountNumber, @AccNum OUTPUT;PRINT @AccNum;
Deleting proceduresThe SQL statement to drop/delete a procedure is:Drop proc <procedureName>Example:Drop proc addNumbers;Go;
FunctionsWhat is a Function?A Function is a set of sql statements which aim to accomplish the same task.How is a function different from a procedure?  A Function is more clear, organized and structured
  It is easy to modify
  It is easy to handle return values than in procedures. In procedures, a temporary table might be necessary to carry a return value and join it with an existing table. This becomes unnecessary if functions are used instead.FunctionsUser-defined functions are routines written using Transact-SQL or the Dot NET Common Language Runtime. Transact-SQL:Transact-SQL (T-SQL) is an extension to SQL, created by Microsoft' and Sybase.Transact-SQL enhances SQL with these additional features: Enhancements to DELETE and UPDATE statements
 Control-of-flow language
 Local variables
 Various support functions for string processing, date processing, mathematics, etc.Dot Net Common Language Runtime(CLR):It is a run-time environment which runs code and provides services that make development process much easier.
Functions are betterThe Advantages of using functions in SQL Server 2008:
FunctionsThe SQL Syntax for creating a function resembles that of a procedure:Create function <functionName>(argumentList)  Returns <returnValueType>  As	Begin	…statements…	Return <returnValue>  EndGoLet us look into some examples.

More Related Content

What's hot (20)

PPT
SQL Queries
Nilt1234
 
PPTX
Triggers
Pooja Dixit
 
PPT
Joins in SQL
Vigneshwaran Sankaran
 
PDF
MySQL developing Store Procedure
Marco Tusa
 
PPTX
Introduction to SQL
Ehsan Hamzei
 
PDF
Sql Basics | Edureka
Edureka!
 
PPT
Sql operators & functions 3
Dr. C.V. Suresh Babu
 
PPTX
Packages in PL/SQL
Pooja Dixit
 
PPTX
Sql commands
Pooja Dixit
 
PPTX
Introduction to triggers
Syed Awais Mazhar Bukhari
 
PPT
MySQL and its basic commands
Bwsrang Basumatary
 
PPTX
Dbms normalization
Pratik Devmurari
 
PPTX
Linked list
akshat360
 
PPTX
Basic SQL and History
SomeshwarMoholkar
 
ODP
Ms sql-server
Md.Mojibul Hoque
 
PPTX
Chapter 1 introduction to sql server
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Procedure and Functions in pl/sql
Ñirmal Tatiwal
 
PPT
Sql Tutorials
Priyabrat Kar
 
PPTX
Trigger
VForce Infotech
 
SQL Queries
Nilt1234
 
Triggers
Pooja Dixit
 
Joins in SQL
Vigneshwaran Sankaran
 
MySQL developing Store Procedure
Marco Tusa
 
Introduction to SQL
Ehsan Hamzei
 
Sql Basics | Edureka
Edureka!
 
Sql operators & functions 3
Dr. C.V. Suresh Babu
 
Packages in PL/SQL
Pooja Dixit
 
Sql commands
Pooja Dixit
 
Introduction to triggers
Syed Awais Mazhar Bukhari
 
MySQL and its basic commands
Bwsrang Basumatary
 
Dbms normalization
Pratik Devmurari
 
Linked list
akshat360
 
Basic SQL and History
SomeshwarMoholkar
 
Ms sql-server
Md.Mojibul Hoque
 
Chapter 1 introduction to sql server
baabtra.com - No. 1 supplier of quality freshers
 
Procedure and Functions in pl/sql
Ñirmal Tatiwal
 
Sql Tutorials
Priyabrat Kar
 

Viewers also liked (20)

PDF
Stored-Procedures-Presentation
Chuck Walker
 
PPTX
Stored procedure
Deepak Sharma
 
PPTX
Cursors, triggers, procedures
Vaibhav Kathuria
 
PPTX
SQL JOIN
Ritwik Das
 
PPT
SQL Tutorial - Basic Commands
1keydata
 
PPT
Sql ppt
Anuja Lad
 
PDF
Python functional programming
Geison Goes
 
PDF
Stored Procedures - SQL
Jorge Jeffrey Vargas Ipince
 
PPT
SQl
sarankumarv
 
PPS
Procedures functions structures in VB.Net
tjunicornfx
 
DOCX
retrieving data using SQL statements
Arun Nair
 
PPTX
Cursor & Function trong SQL Server
Huy Vũ
 
PPTX
VB Function and procedure
pragya ratan
 
PDF
SQL Cursor - kiểu dữ liệu Cursor (Kiểu dữ liệu con trỏ)
Pix Nhox
 
PDF
Introduction to SQL
Ram Kedem
 
PPTX
Sub-queries,Groupby and having in SQL
Vikash Sharma
 
PPTX
Procedure text
inantia
 
PDF
SQL Server
webhostingguy
 
Stored-Procedures-Presentation
Chuck Walker
 
Stored procedure
Deepak Sharma
 
Cursors, triggers, procedures
Vaibhav Kathuria
 
SQL JOIN
Ritwik Das
 
SQL Tutorial - Basic Commands
1keydata
 
Sql ppt
Anuja Lad
 
Python functional programming
Geison Goes
 
Stored Procedures - SQL
Jorge Jeffrey Vargas Ipince
 
Procedures functions structures in VB.Net
tjunicornfx
 
retrieving data using SQL statements
Arun Nair
 
Cursor & Function trong SQL Server
Huy Vũ
 
VB Function and procedure
pragya ratan
 
SQL Cursor - kiểu dữ liệu Cursor (Kiểu dữ liệu con trỏ)
Pix Nhox
 
Introduction to SQL
Ram Kedem
 
Sub-queries,Groupby and having in SQL
Vikash Sharma
 
Procedure text
inantia
 
SQL Server
webhostingguy
 
Ad

Similar to Sql Functions And Procedures (20)

PPT
Module04
Sridhar P
 
PPT
Intro to tsql
Syed Asrarali
 
PPT
Intro to tsql unit 14
Syed Asrarali
 
PPTX
Stored procedures
MuksNoor
 
PPTX
Unit 3
Abha Damani
 
PPT
SQL Server 2000 Research Series - Transact SQL
Jerry Yang
 
PPTX
Procedures and triggers in SQL
Vikash Sharma
 
PDF
Lecture Notes Unit5 chapter17 Stored procedures and functions
Murugan146644
 
PPTX
Store procedures
Farzan Wadood
 
PPS
09 qmds2005 session13
Niit Care
 
PPTX
Unit 3(rdbms)
Jay Patel
 
PPTX
Unit 3(rdbms)
Jay Patel
 
PPTX
Oracle: Procedures
DataminingTools Inc
 
PPTX
Oracle: Procedures
oracle content
 
PPTX
DBMS: Week 11 - Stored Procedures and Functions
RashidFaridChishti
 
PDF
SQL Procedures & Functions
JeevananthamArumugam
 
PPTX
Lecture 3.2_Subprogrammm - Function.pptx
pproychd
 
Module04
Sridhar P
 
Intro to tsql
Syed Asrarali
 
Intro to tsql unit 14
Syed Asrarali
 
Stored procedures
MuksNoor
 
Unit 3
Abha Damani
 
SQL Server 2000 Research Series - Transact SQL
Jerry Yang
 
Procedures and triggers in SQL
Vikash Sharma
 
Lecture Notes Unit5 chapter17 Stored procedures and functions
Murugan146644
 
Store procedures
Farzan Wadood
 
09 qmds2005 session13
Niit Care
 
Unit 3(rdbms)
Jay Patel
 
Unit 3(rdbms)
Jay Patel
 
Oracle: Procedures
DataminingTools Inc
 
Oracle: Procedures
oracle content
 
DBMS: Week 11 - Stored Procedures and Functions
RashidFaridChishti
 
SQL Procedures & Functions
JeevananthamArumugam
 
Lecture 3.2_Subprogrammm - Function.pptx
pproychd
 
Ad

More from DataminingTools Inc (20)

PPTX
Terminology Machine Learning
DataminingTools Inc
 
PPTX
Techniques Machine Learning
DataminingTools Inc
 
PPTX
Machine learning Introduction
DataminingTools Inc
 
PPTX
Areas of machine leanring
DataminingTools Inc
 
PPTX
AI: Planning and AI
DataminingTools Inc
 
PPTX
AI: Logic in AI 2
DataminingTools Inc
 
PPTX
AI: Logic in AI
DataminingTools Inc
 
PPTX
AI: Learning in AI 2
DataminingTools Inc
 
PPTX
AI: Learning in AI
DataminingTools Inc
 
PPTX
AI: Introduction to artificial intelligence
DataminingTools Inc
 
PPTX
AI: Belief Networks
DataminingTools Inc
 
PPTX
AI: AI & Searching
DataminingTools Inc
 
PPTX
AI: AI & Problem Solving
DataminingTools Inc
 
PPTX
Data Mining: Text and web mining
DataminingTools Inc
 
PPTX
Data Mining: Outlier analysis
DataminingTools Inc
 
PPTX
Data Mining: Mining stream time series and sequence data
DataminingTools Inc
 
PPTX
Data Mining: Mining ,associations, and correlations
DataminingTools Inc
 
PPTX
Data Mining: Graph mining and social network analysis
DataminingTools Inc
 
PPTX
Data warehouse and olap technology
DataminingTools Inc
 
PPTX
Data Mining: Data processing
DataminingTools Inc
 
Terminology Machine Learning
DataminingTools Inc
 
Techniques Machine Learning
DataminingTools Inc
 
Machine learning Introduction
DataminingTools Inc
 
Areas of machine leanring
DataminingTools Inc
 
AI: Planning and AI
DataminingTools Inc
 
AI: Logic in AI 2
DataminingTools Inc
 
AI: Logic in AI
DataminingTools Inc
 
AI: Learning in AI 2
DataminingTools Inc
 
AI: Learning in AI
DataminingTools Inc
 
AI: Introduction to artificial intelligence
DataminingTools Inc
 
AI: Belief Networks
DataminingTools Inc
 
AI: AI & Searching
DataminingTools Inc
 
AI: AI & Problem Solving
DataminingTools Inc
 
Data Mining: Text and web mining
DataminingTools Inc
 
Data Mining: Outlier analysis
DataminingTools Inc
 
Data Mining: Mining stream time series and sequence data
DataminingTools Inc
 
Data Mining: Mining ,associations, and correlations
DataminingTools Inc
 
Data Mining: Graph mining and social network analysis
DataminingTools Inc
 
Data warehouse and olap technology
DataminingTools Inc
 
Data Mining: Data processing
DataminingTools Inc
 

Recently uploaded (20)

PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
PDF
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
PPTX
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PPTX
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
PDF
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PPTX
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PDF
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 

Sql Functions And Procedures

  • 2. SQL ProceduresWhat is an SQL Procedure? An SQL Procedure contains a group of sql statements which solve a common purpose. Syntax:Consider a simple SQL Procedure:Create proc printProcedureAsPrint ‘Hello World’goCreate proc <procedureName>As…. Statements…Print is a command in SQL Server 2008. It is used to print a string on the screen.NOTE: The SQL Procedures that we learn here are stored in the system by the DBMS. They are hence known as stored procedures
  • 3. Altering stored proceduresHow to alter a SQL stored procedure? A Stored procedure can be altered using the alter proc command.Syntax:Consider a simple SQL Procedure:Alter proc printProcedureAsPrint ‘New Hello World’goAlter proc<procedureName>As…. New Statements…
  • 4. Executing ProceduresThe Main advantage of using a stored procedure is its reusability, i.e., a procedure can be called any time that it needs to be executed.An SQL procedure can be executed using the exec command:Exec <ProcedureName>Example: Create proc printProcedureAsPrint ‘New Hello World’GoExec printProcedureOUTPUT:New Hello World
  • 5. Procedure ParametersA Procedure, like a C or C++ Procedure(function) can take up input parameters and produce return values.Output Parameters (Returned Values)SQL ProcedureInput ParametersA Procedure with Input Parameters:Create proc hellop@info1 int= 1,@info2 intAsPrint @info1+@info2Calling a Procedure with parameters:Exec proc hellop 3,2
  • 6. Procedure ParametersCalling a Procedure with parameters:Exec proc hellop 3,2Here, the value ‘3’ over-rides the default value. If a paramater does not have a default value defined for it, then the DBMS requires the user to pass a value for it before execution.Eg: exec proc hellop 1: will return an error as the second parameter is not assigned any value.
  • 7. Procedure ParametersOutput Parameters:A Procedure can have output parameters also. The Output parameters are specified by the keyword output.Consider a procedure to add two numbers:Create proc adder@num1 int =0,@num2 int =0,AsDeclare @num3 int= @num1 + @num2;Print @num3;GoExecuting the procedure: exec proc adder 3,4 Output: 7A Procedure can also return a value as: return <value>NOTEThe Data members in SQL (@num1) resemble the variables in c/c++. But there is a big difference: They can’t be altered directly.Eg: @num1 = @num1 +1 Is invalid. The values are assigned at the time of their declaration only.
  • 8. Procedure ParametersUsing the Output parameters:CREATE PROC Sales @CustomerIDint, @AccNumvarchar(10) OUTPUTASSELECT @AccNum = AccNum FROM Sales.Customer WHERE CustomerID = @CustomerID;GOCalling a procedure with return values: DECLARE @AccNumvarchar(10);EXEC GetCustomerAccountNumber, @AccNum OUTPUT;PRINT @AccNum;
  • 9. Deleting proceduresThe SQL statement to drop/delete a procedure is:Drop proc <procedureName>Example:Drop proc addNumbers;Go;
  • 10. FunctionsWhat is a Function?A Function is a set of sql statements which aim to accomplish the same task.How is a function different from a procedure? A Function is more clear, organized and structured
  • 11. It is easy to modify
  • 12. It is easy to handle return values than in procedures. In procedures, a temporary table might be necessary to carry a return value and join it with an existing table. This becomes unnecessary if functions are used instead.FunctionsUser-defined functions are routines written using Transact-SQL or the Dot NET Common Language Runtime. Transact-SQL:Transact-SQL (T-SQL) is an extension to SQL, created by Microsoft' and Sybase.Transact-SQL enhances SQL with these additional features: Enhancements to DELETE and UPDATE statements
  • 15. Various support functions for string processing, date processing, mathematics, etc.Dot Net Common Language Runtime(CLR):It is a run-time environment which runs code and provides services that make development process much easier.
  • 16. Functions are betterThe Advantages of using functions in SQL Server 2008:
  • 17. FunctionsThe SQL Syntax for creating a function resembles that of a procedure:Create function <functionName>(argumentList) Returns <returnValueType> As Begin …statements… Return <returnValue> EndGoLet us look into some examples.
  • 18. FunctionsA Sample function:Consider a function that takes two numbers and finds their sum:Create function adder(@num1 int,@num2 int)Returns intAsBeginDeclare @num3 int;Set @num3 = @num1+@num2;Return @num3;EndgoNote that the SET keyword is used to change the value of a variableCalling the function:The Prefix dbo instructs the DBMS that you are the database ownerPrint dbo.adder(2,4)go
  • 19. Deleting FunctionsThe SQL statement to drop/delete a function is similar to that of a procedure:Drop function <procedureName>Example:Drop function addNumbers;Go;
  • 21. Creating Procedures
  • 22. Modifying existing procedures
  • 23. Deleting procedures
  • 25. Creating functions
  • 26. Modifying existing functions
  • 27. Deleting functionsVisit more self help tutorialsPick a tutorial of your choice and browse through it at your own pace.The tutorials section is free, self-guiding and will not involve any additional support.Visit us at www.dataminingtools.net