SlideShare a Scribd company logo
1 of 17
12 SQL SERVER: SQLFUNCTIONS AND PROCEDURES
SQL Procedures What 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 printProcedure As Print ‘Hello World’ go Create 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 procedures How 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 printProcedure As Print ‘New Hello World’ go Alter proc<procedureName> As …. New Statements…
Executing Procedures The 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 printProcedure As Print ‘New Hello World’ Go Exec printProcedure OUTPUT: New Hello World
Procedure Parameters A Procedure, like a C or C++ Procedure(function) can take up input parameters and produce return values. Output Parameters (Returned Values) SQL Procedure Input Parameters A Procedure with Input Parameters: Create proc hellop @info1 int= 1, @info2 int As Print @info1+@info2 Calling a Procedure with parameters: Exec proc hellop 3,2
Procedure Parameters Calling a Procedure with parameters: Exec proc hellop 3,2 Here, 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 Parameters Output 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, As Declare @num3 int= @num1 + @num2; Print @num3; Go Executing the procedure: exec proc adder 3,4  Output: 7 A Procedure can also return a value as: return <value> NOTE The 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 Parameters Using the Output parameters: CREATE PROC Sales   @CustomerIDint,   @AccNumvarchar(10) OUTPUT AS SELECT @AccNum = AccNum  FROM Sales.Customer  WHERE CustomerID = @CustomerID; GO Calling a procedure with return values:  DECLARE @AccNumvarchar(10); EXEC GetCustomerAccountNumber, @AccNum OUTPUT; PRINT @AccNum;
Deleting procedures The SQL statement to drop/delete a procedure is: Drop proc <procedureName> Example: Drop proc addNumbers; Go;
Functions What 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? ,[object Object]
  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.,[object Object]
 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 better The Advantages of using functions in SQL Server 2008:
Functions The SQL Syntax for creating a function resembles that of a procedure: Create function <functionName> (argumentList)   Returns <returnValueType>   As 	Begin 	…statements… 	Return <returnValue>   End Go Let us look into some examples.

More Related Content

What's hot (20)

SQL Server Stored procedures
SQL Server Stored proceduresSQL Server Stored procedures
SQL Server Stored procedures
 
Stored procedure
Stored procedureStored procedure
Stored procedure
 
Lecture 2. MS SQL. Stored procedures.
Lecture 2. MS SQL. Stored procedures.Lecture 2. MS SQL. Stored procedures.
Lecture 2. MS SQL. Stored procedures.
 
Chapter 3 stored procedures
Chapter 3 stored proceduresChapter 3 stored procedures
Chapter 3 stored procedures
 
Constraints In Sql
Constraints In SqlConstraints In Sql
Constraints In Sql
 
Introduction to sql
Introduction to sqlIntroduction to sql
Introduction to sql
 
pl/sql Procedure
pl/sql Procedurepl/sql Procedure
pl/sql Procedure
 
DATABASE CONSTRAINTS
DATABASE CONSTRAINTSDATABASE CONSTRAINTS
DATABASE CONSTRAINTS
 
Sql commands
Sql commandsSql commands
Sql commands
 
SQL
SQLSQL
SQL
 
Types Of Join In Sql Server - Join With Example In Sql Server
Types Of Join In Sql Server - Join With Example In Sql ServerTypes Of Join In Sql Server - Join With Example In Sql Server
Types Of Join In Sql Server - Join With Example In Sql Server
 
SQL - Structured query language introduction
SQL - Structured query language introductionSQL - Structured query language introduction
SQL - Structured query language introduction
 
1 - Introduction to PL/SQL
1 - Introduction to PL/SQL1 - Introduction to PL/SQL
1 - Introduction to PL/SQL
 
Stored-Procedures-Presentation
Stored-Procedures-PresentationStored-Procedures-Presentation
Stored-Procedures-Presentation
 
SQL Commands
SQL Commands SQL Commands
SQL Commands
 
Introduction to Mysql
Introduction to MysqlIntroduction to Mysql
Introduction to Mysql
 
Index in sql server
Index in sql serverIndex in sql server
Index in sql server
 
Triggers in SQL | Edureka
Triggers in SQL | EdurekaTriggers in SQL | Edureka
Triggers in SQL | Edureka
 
Stored procedure
Stored procedureStored procedure
Stored procedure
 
Postgresql
PostgresqlPostgresql
Postgresql
 

Viewers also liked

Cursors, triggers, procedures
Cursors, triggers, proceduresCursors, triggers, procedures
Cursors, triggers, proceduresVaibhav Kathuria
 
SQL Tutorial - Basic Commands
SQL Tutorial - Basic CommandsSQL Tutorial - Basic Commands
SQL Tutorial - Basic Commands1keydata
 
Python functional programming
Python functional programmingPython functional programming
Python functional programmingGeison Goes
 
Procedures functions structures in VB.Net
Procedures  functions  structures in VB.NetProcedures  functions  structures in VB.Net
Procedures functions structures in VB.Nettjunicornfx
 
retrieving data using SQL statements
retrieving data using SQL statementsretrieving data using SQL statements
retrieving data using SQL statementsArun Nair
 
Cursor & Function trong SQL Server
Cursor & Function trong SQL ServerCursor & Function trong SQL Server
Cursor & Function trong SQL ServerHuy Vũ
 
VB Function and procedure
VB Function and procedureVB Function and procedure
VB Function and procedurepragya ratan
 
SQL Cursor - kiểu dữ liệu Cursor (Kiểu dữ liệu con trỏ)
SQL Cursor - kiểu dữ liệu Cursor (Kiểu dữ liệu con trỏ)SQL Cursor - kiểu dữ liệu Cursor (Kiểu dữ liệu con trỏ)
SQL Cursor - kiểu dữ liệu Cursor (Kiểu dữ liệu con trỏ)Pix Nhox
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQLRam Kedem
 
Sub-queries,Groupby and having in SQL
Sub-queries,Groupby and having in SQLSub-queries,Groupby and having in SQL
Sub-queries,Groupby and having in SQLVikash Sharma
 
Procedure text
Procedure textProcedure text
Procedure textinantia
 
SQL – A Tutorial I
SQL – A Tutorial  ISQL – A Tutorial  I
SQL – A Tutorial IGagan Deep
 

Viewers also liked (20)

SQL Functions
SQL FunctionsSQL Functions
SQL Functions
 
Cursors, triggers, procedures
Cursors, triggers, proceduresCursors, triggers, procedures
Cursors, triggers, procedures
 
SQL JOIN
SQL JOINSQL JOIN
SQL JOIN
 
SQL Tutorial - Basic Commands
SQL Tutorial - Basic CommandsSQL Tutorial - Basic Commands
SQL Tutorial - Basic Commands
 
Sql ppt
Sql pptSql ppt
Sql ppt
 
Python functional programming
Python functional programmingPython functional programming
Python functional programming
 
Stored Procedures - SQL
Stored Procedures - SQLStored Procedures - SQL
Stored Procedures - SQL
 
SQl
SQlSQl
SQl
 
Procedures functions structures in VB.Net
Procedures  functions  structures in VB.NetProcedures  functions  structures in VB.Net
Procedures functions structures in VB.Net
 
retrieving data using SQL statements
retrieving data using SQL statementsretrieving data using SQL statements
retrieving data using SQL statements
 
Cursor & Function trong SQL Server
Cursor & Function trong SQL ServerCursor & Function trong SQL Server
Cursor & Function trong SQL Server
 
VB Function and procedure
VB Function and procedureVB Function and procedure
VB Function and procedure
 
SQL Cursor - kiểu dữ liệu Cursor (Kiểu dữ liệu con trỏ)
SQL Cursor - kiểu dữ liệu Cursor (Kiểu dữ liệu con trỏ)SQL Cursor - kiểu dữ liệu Cursor (Kiểu dữ liệu con trỏ)
SQL Cursor - kiểu dữ liệu Cursor (Kiểu dữ liệu con trỏ)
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQL
 
Sub-queries,Groupby and having in SQL
Sub-queries,Groupby and having in SQLSub-queries,Groupby and having in SQL
Sub-queries,Groupby and having in SQL
 
Procedure text
Procedure textProcedure text
Procedure text
 
SQL Server
SQL ServerSQL Server
SQL Server
 
Store procedures
Store proceduresStore procedures
Store procedures
 
SQL – A Tutorial I
SQL – A Tutorial  ISQL – A Tutorial  I
SQL – A Tutorial I
 
Sql Basics And Advanced
Sql Basics And AdvancedSql Basics And Advanced
Sql Basics And Advanced
 

Similar to Sql Functions And Procedures

Unit 3(rdbms)
Unit 3(rdbms)Unit 3(rdbms)
Unit 3(rdbms)Jay Patel
 
Unit 3(rdbms)
Unit 3(rdbms)Unit 3(rdbms)
Unit 3(rdbms)Jay Patel
 
Watch Re-runs on your SQL Server with RML Utilities
Watch Re-runs on your SQL Server with RML UtilitiesWatch Re-runs on your SQL Server with RML Utilities
Watch Re-runs on your SQL Server with RML Utilitiesdpcobb
 
SQL Server 2000 Research Series - Transact SQL
SQL Server 2000 Research Series - Transact SQLSQL Server 2000 Research Series - Transact SQL
SQL Server 2000 Research Series - Transact SQLJerry Yang
 
Evolutionary db development
Evolutionary db development Evolutionary db development
Evolutionary db development Open Party
 
New features of sql server 2005
New features of sql server 2005New features of sql server 2005
New features of sql server 2005Govind Raj
 
Procedures and triggers in SQL
Procedures and triggers in SQLProcedures and triggers in SQL
Procedures and triggers in SQLVikash Sharma
 
Intro to tsql unit 14
Intro to tsql   unit 14Intro to tsql   unit 14
Intro to tsql unit 14Syed Asrarali
 
Procedures/functions of rdbms
Procedures/functions of rdbmsProcedures/functions of rdbms
Procedures/functions of rdbmsjain.pralabh
 
PLSQLmy Updated (1).pptx
PLSQLmy Updated (1).pptxPLSQLmy Updated (1).pptx
PLSQLmy Updated (1).pptxvamsiyadav39
 
Introduction to Threading in .Net
Introduction to Threading in .NetIntroduction to Threading in .Net
Introduction to Threading in .Netwebhostingguy
 
Tony Jambu (obscure) tools of the trade for tuning oracle sq ls
Tony Jambu   (obscure) tools of the trade for tuning oracle sq lsTony Jambu   (obscure) tools of the trade for tuning oracle sq ls
Tony Jambu (obscure) tools of the trade for tuning oracle sq lsInSync Conference
 

Similar to Sql Functions And Procedures (20)

Unit 3(rdbms)
Unit 3(rdbms)Unit 3(rdbms)
Unit 3(rdbms)
 
Unit 3(rdbms)
Unit 3(rdbms)Unit 3(rdbms)
Unit 3(rdbms)
 
Unit 3
Unit 3Unit 3
Unit 3
 
Watch Re-runs on your SQL Server with RML Utilities
Watch Re-runs on your SQL Server with RML UtilitiesWatch Re-runs on your SQL Server with RML Utilities
Watch Re-runs on your SQL Server with RML Utilities
 
SQL Server 2000 Research Series - Transact SQL
SQL Server 2000 Research Series - Transact SQLSQL Server 2000 Research Series - Transact SQL
SQL Server 2000 Research Series - Transact SQL
 
Module04
Module04Module04
Module04
 
Evolutionary db development
Evolutionary db development Evolutionary db development
Evolutionary db development
 
New features of sql server 2005
New features of sql server 2005New features of sql server 2005
New features of sql server 2005
 
Procedures and triggers in SQL
Procedures and triggers in SQLProcedures and triggers in SQL
Procedures and triggers in SQL
 
Intro to tsql
Intro to tsqlIntro to tsql
Intro to tsql
 
Intro to tsql unit 14
Intro to tsql   unit 14Intro to tsql   unit 14
Intro to tsql unit 14
 
Data Access with JDBC
Data Access with JDBCData Access with JDBC
Data Access with JDBC
 
Procedure n functions
Procedure n functionsProcedure n functions
Procedure n functions
 
Procedures/functions of rdbms
Procedures/functions of rdbmsProcedures/functions of rdbms
Procedures/functions of rdbms
 
PLSQLmy Updated (1).pptx
PLSQLmy Updated (1).pptxPLSQLmy Updated (1).pptx
PLSQLmy Updated (1).pptx
 
Introduction to Threading in .Net
Introduction to Threading in .NetIntroduction to Threading in .Net
Introduction to Threading in .Net
 
Tony Jambu (obscure) tools of the trade for tuning oracle sq ls
Tony Jambu   (obscure) tools of the trade for tuning oracle sq lsTony Jambu   (obscure) tools of the trade for tuning oracle sq ls
Tony Jambu (obscure) tools of the trade for tuning oracle sq ls
 
Msql
Msql Msql
Msql
 
Oracle: Procedures
Oracle: ProceduresOracle: Procedures
Oracle: Procedures
 
Oracle: Procedures
Oracle: ProceduresOracle: Procedures
Oracle: Procedures
 

More from DataminingTools Inc

AI: Introduction to artificial intelligence
AI: Introduction to artificial intelligenceAI: Introduction to artificial intelligence
AI: Introduction to artificial intelligenceDataminingTools Inc
 
Data Mining: Text and web mining
Data Mining: Text and web miningData Mining: Text and web mining
Data Mining: Text and web miningDataminingTools Inc
 
Data Mining: Mining stream time series and sequence data
Data Mining: Mining stream time series and sequence dataData Mining: Mining stream time series and sequence data
Data Mining: Mining stream time series and sequence dataDataminingTools Inc
 
Data Mining: Mining ,associations, and correlations
Data Mining: Mining ,associations, and correlationsData Mining: Mining ,associations, and correlations
Data Mining: Mining ,associations, and correlationsDataminingTools Inc
 
Data Mining: Graph mining and social network analysis
Data Mining: Graph mining and social network analysisData Mining: Graph mining and social network analysis
Data Mining: Graph mining and social network analysisDataminingTools Inc
 
Data warehouse and olap technology
Data warehouse and olap technologyData warehouse and olap technology
Data warehouse and olap technologyDataminingTools Inc
 

More from DataminingTools Inc (20)

Terminology Machine Learning
Terminology Machine LearningTerminology Machine Learning
Terminology Machine Learning
 
Techniques Machine Learning
Techniques Machine LearningTechniques Machine Learning
Techniques Machine Learning
 
Machine learning Introduction
Machine learning IntroductionMachine learning Introduction
Machine learning Introduction
 
Areas of machine leanring
Areas of machine leanringAreas of machine leanring
Areas of machine leanring
 
AI: Planning and AI
AI: Planning and AIAI: Planning and AI
AI: Planning and AI
 
AI: Logic in AI 2
AI: Logic in AI 2AI: Logic in AI 2
AI: Logic in AI 2
 
AI: Logic in AI
AI: Logic in AIAI: Logic in AI
AI: Logic in AI
 
AI: Learning in AI 2
AI: Learning in AI 2AI: Learning in AI 2
AI: Learning in AI 2
 
AI: Learning in AI
AI: Learning in AI AI: Learning in AI
AI: Learning in AI
 
AI: Introduction to artificial intelligence
AI: Introduction to artificial intelligenceAI: Introduction to artificial intelligence
AI: Introduction to artificial intelligence
 
AI: Belief Networks
AI: Belief NetworksAI: Belief Networks
AI: Belief Networks
 
AI: AI & Searching
AI: AI & SearchingAI: AI & Searching
AI: AI & Searching
 
AI: AI & Problem Solving
AI: AI & Problem SolvingAI: AI & Problem Solving
AI: AI & Problem Solving
 
Data Mining: Text and web mining
Data Mining: Text and web miningData Mining: Text and web mining
Data Mining: Text and web mining
 
Data Mining: Outlier analysis
Data Mining: Outlier analysisData Mining: Outlier analysis
Data Mining: Outlier analysis
 
Data Mining: Mining stream time series and sequence data
Data Mining: Mining stream time series and sequence dataData Mining: Mining stream time series and sequence data
Data Mining: Mining stream time series and sequence data
 
Data Mining: Mining ,associations, and correlations
Data Mining: Mining ,associations, and correlationsData Mining: Mining ,associations, and correlations
Data Mining: Mining ,associations, and correlations
 
Data Mining: Graph mining and social network analysis
Data Mining: Graph mining and social network analysisData Mining: Graph mining and social network analysis
Data Mining: Graph mining and social network analysis
 
Data warehouse and olap technology
Data warehouse and olap technologyData warehouse and olap technology
Data warehouse and olap technology
 
Data Mining: Data processing
Data Mining: Data processingData Mining: Data processing
Data Mining: Data processing
 

Recently uploaded

Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfSeasiaInfotech2
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesZilliz
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 

Recently uploaded (20)

Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdf
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector Databases
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 

Sql Functions And Procedures

  • 1. 12 SQL SERVER: SQLFUNCTIONS AND PROCEDURES
  • 2. SQL Procedures What 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 printProcedure As Print ‘Hello World’ go Create 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 procedures How 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 printProcedure As Print ‘New Hello World’ go Alter proc<procedureName> As …. New Statements…
  • 4. Executing Procedures The 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 printProcedure As Print ‘New Hello World’ Go Exec printProcedure OUTPUT: New Hello World
  • 5. Procedure Parameters A Procedure, like a C or C++ Procedure(function) can take up input parameters and produce return values. Output Parameters (Returned Values) SQL Procedure Input Parameters A Procedure with Input Parameters: Create proc hellop @info1 int= 1, @info2 int As Print @info1+@info2 Calling a Procedure with parameters: Exec proc hellop 3,2
  • 6. Procedure Parameters Calling a Procedure with parameters: Exec proc hellop 3,2 Here, 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 Parameters Output 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, As Declare @num3 int= @num1 + @num2; Print @num3; Go Executing the procedure: exec proc adder 3,4 Output: 7 A Procedure can also return a value as: return <value> NOTE The 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 Parameters Using the Output parameters: CREATE PROC Sales @CustomerIDint, @AccNumvarchar(10) OUTPUT AS SELECT @AccNum = AccNum FROM Sales.Customer WHERE CustomerID = @CustomerID; GO Calling a procedure with return values: DECLARE @AccNumvarchar(10); EXEC GetCustomerAccountNumber, @AccNum OUTPUT; PRINT @AccNum;
  • 9. Deleting procedures The SQL statement to drop/delete a procedure is: Drop proc <procedureName> Example: Drop proc addNumbers; Go;
  • 10.
  • 11. It is easy to modify
  • 12.
  • 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 better The Advantages of using functions in SQL Server 2008:
  • 17. Functions The SQL Syntax for creating a function resembles that of a procedure: Create function <functionName> (argumentList) Returns <returnValueType> As Begin …statements… Return <returnValue> End Go Let us look into some examples.
  • 18. Functions A Sample function: Consider a function that takes two numbers and finds their sum: Create function adder (@num1 int,@num2 int) Returns int As Begin Declare @num3 int; Set @num3 = @num1+@num2; Return @num3; End go Note that the SET keyword is used to change the value of a variable Calling the function: The Prefix dbo instructs the DBMS that you are the database owner Print dbo.adder(2,4) go
  • 19. Deleting Functions The SQL statement to drop/delete a function is similar to that of a procedure: Drop function <procedureName> Example: Drop function addNumbers; Go;
  • 20.
  • 21. Creating Procedures
  • 22. Modifying existing procedures
  • 23. Deleting procedures
  • 25. Creating functions
  • 26. Modifying existing functions
  • 27.