SlideShare une entreprise Scribd logo
1  sur  22
Introduction to Stored Procedure & Functions  Module 4
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Learning Objectives
[object Object],[object Object],[object Object],[object Object],[object Object],Module 4 : Agenda
[object Object],[object Object],[object Object],[object Object],[object Object],Control Flow Language
Control Flow Language ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],Control Flow Language ,[object Object],[object Object],[object Object],[object Object],[object Object],SELECT  CASE convert(int, (RAND() * 3)) WHEN  0  THEN   ‘ A ’ WHEN  1  THEN   ‘ B ’ ELSE   ‘ E ’ END
[object Object],[object Object],[object Object],Control Flow Language – Local Variable declaration  ,[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],Control Flow Language – Blocks ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
What is a Transaction? ,[object Object],[object Object],[object Object]
Transaction Control ,[object Object],[object Object],[object Object]
COMMIT ,[object Object],[object Object],[object Object],[object Object]
ROLLBACK ,[object Object],[object Object],[object Object]
Transaction Example ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
BuiltIn Variables Returns the maximum number of simultaneous user connections allowed on a Microsoft® SQL Server™. The number returned is not necessarily the number currently configured. @@MAXCONNECTION Returns the name of the language currently in use. @@LANGUAGE Returns the name of the local server running Microsoft® SQL Server™. @@SERVERNAME Returns the number of rows affected by the last statement.  @@ROWCOUNT Returns the last-inserted identity value. @@IDENTITY Returns the error number for the last Transact-SQL statement executed. @@ERROR Description Variable
What is a Stored Procedure? ,[object Object],[object Object],[object Object]
Stored Procedures (Continued) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Benefits of Stored Procedures ,[object Object],[object Object],[object Object],[object Object]
Stored Procedures ,[object Object],[object Object],procedure_name   is the name of the of the procedure @parameter   parameter passed to the procedure data_type  type of data passed to the procedure
Stored Procedure Example ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
User Defined Functions ,[object Object],CREATE FUNCTION Addition(@Number1 Decimal(6,2),   @Number2 Decimal(6,2)) RETURNS Decimal(6,2) BEGIN DECLARE @Result Decimal(6,2) SET @Result = @Number1 + @Number2 RETURN @Result END; GO SELECT dbo.addition(2,2)
Key Points ,[object Object],[object Object],[object Object]
Questions & Comments

Contenu connexe

Tendances

Intro to tsql unit 15
Intro to tsql   unit 15Intro to tsql   unit 15
Intro to tsql unit 15Syed Asrarali
 
Oracle - Program with PL/SQL - Lession 17
Oracle - Program with PL/SQL - Lession 17Oracle - Program with PL/SQL - Lession 17
Oracle - Program with PL/SQL - Lession 17Thuan Nguyen
 
Programming in Oracle with PL/SQL
Programming in Oracle with PL/SQLProgramming in Oracle with PL/SQL
Programming in Oracle with PL/SQLlubna19
 
Oracle - Program with PL/SQL - Lession 01
Oracle - Program with PL/SQL - Lession 01Oracle - Program with PL/SQL - Lession 01
Oracle - Program with PL/SQL - Lession 01Thuan Nguyen
 
Oracle - Program with PL/SQL - Lession 11
Oracle - Program with PL/SQL - Lession 11Oracle - Program with PL/SQL - Lession 11
Oracle - Program with PL/SQL - Lession 11Thuan Nguyen
 
Sql storeprocedure
Sql storeprocedureSql storeprocedure
Sql storeprocedureftz 420
 
Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09Thuan Nguyen
 
Stored-Procedures-Presentation
Stored-Procedures-PresentationStored-Procedures-Presentation
Stored-Procedures-PresentationChuck Walker
 

Tendances (19)

Packages - PL/SQL
Packages - PL/SQLPackages - PL/SQL
Packages - PL/SQL
 
Intro to tsql unit 15
Intro to tsql   unit 15Intro to tsql   unit 15
Intro to tsql unit 15
 
PL/SQL
PL/SQLPL/SQL
PL/SQL
 
Oracle - Program with PL/SQL - Lession 17
Oracle - Program with PL/SQL - Lession 17Oracle - Program with PL/SQL - Lession 17
Oracle - Program with PL/SQL - Lession 17
 
Procedure n functions
Procedure n functionsProcedure n functions
Procedure n functions
 
Programming in Oracle with PL/SQL
Programming in Oracle with PL/SQLProgramming in Oracle with PL/SQL
Programming in Oracle with PL/SQL
 
Oracle - Program with PL/SQL - Lession 01
Oracle - Program with PL/SQL - Lession 01Oracle - Program with PL/SQL - Lession 01
Oracle - Program with PL/SQL - Lession 01
 
Oracle - Program with PL/SQL - Lession 11
Oracle - Program with PL/SQL - Lession 11Oracle - Program with PL/SQL - Lession 11
Oracle - Program with PL/SQL - Lession 11
 
PL/SQL TRIGGERS
PL/SQL TRIGGERSPL/SQL TRIGGERS
PL/SQL TRIGGERS
 
Sql storeprocedure
Sql storeprocedureSql storeprocedure
Sql storeprocedure
 
Function and types
Function  and typesFunction  and types
Function and types
 
Function
FunctionFunction
Function
 
Store procedures
Store proceduresStore procedures
Store procedures
 
Oracle PL sql 3
Oracle PL sql 3Oracle PL sql 3
Oracle PL sql 3
 
Functions
FunctionsFunctions
Functions
 
Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09
 
Intro to tsql
Intro to tsqlIntro to tsql
Intro to tsql
 
Stored-Procedures-Presentation
Stored-Procedures-PresentationStored-Procedures-Presentation
Stored-Procedures-Presentation
 
Sql Functions And Procedures
Sql Functions And ProceduresSql Functions And Procedures
Sql Functions And Procedures
 

En vedette

High Performance Front-End Development
High Performance Front-End DevelopmentHigh Performance Front-End Development
High Performance Front-End Developmentdrywallbmb
 
Multidimensional model programming
Multidimensional model programmingMultidimensional model programming
Multidimensional model programmingSteve Xu
 
Spatialware_2_Sql08
Spatialware_2_Sql08Spatialware_2_Sql08
Spatialware_2_Sql08Mike Osbourn
 
Transact sql data definition language - ddl- reference
Transact sql data definition language - ddl- referenceTransact sql data definition language - ddl- reference
Transact sql data definition language - ddl- referenceSteve Xu
 
SQL Server 2008 for .NET Developers
SQL Server 2008 for .NET DevelopersSQL Server 2008 for .NET Developers
SQL Server 2008 for .NET Developersllangit
 
Multi-thematic spatial databases
Multi-thematic spatial databasesMulti-thematic spatial databases
Multi-thematic spatial databasesConor Mc Elhinney
 
Sql server ___________session3-normailzation
Sql server  ___________session3-normailzationSql server  ___________session3-normailzation
Sql server ___________session3-normailzationEhtisham Ali
 
Alasql - база данных SQL на JavaScript (MoscowJS)
Alasql - база данных SQL на JavaScript (MoscowJS)Alasql - база данных SQL на JavaScript (MoscowJS)
Alasql - база данных SQL на JavaScript (MoscowJS)Andrey Gershun
 
X query language reference
X query language referenceX query language reference
X query language referenceSteve Xu
 
Css introduction
Css introductionCss introduction
Css introductionSridhar P
 
AlaSQL библиотека для обработки JavaScript данных (презентация для ForntEnd 2...
AlaSQL библиотека для обработки JavaScript данных (презентация для ForntEnd 2...AlaSQL библиотека для обработки JavaScript данных (презентация для ForntEnd 2...
AlaSQL библиотека для обработки JavaScript данных (презентация для ForntEnd 2...Andrey Gershun
 
5 tsssisu sql_server_2012
5 tsssisu sql_server_20125 tsssisu sql_server_2012
5 tsssisu sql_server_2012Steve Xu
 
SQL Server 2008 for Developers
SQL Server 2008 for DevelopersSQL Server 2008 for Developers
SQL Server 2008 for Developersllangit
 

En vedette (20)

High Performance Front-End Development
High Performance Front-End DevelopmentHigh Performance Front-End Development
High Performance Front-End Development
 
Multidimensional model programming
Multidimensional model programmingMultidimensional model programming
Multidimensional model programming
 
Spatialware_2_Sql08
Spatialware_2_Sql08Spatialware_2_Sql08
Spatialware_2_Sql08
 
Module06
Module06Module06
Module06
 
Transact sql data definition language - ddl- reference
Transact sql data definition language - ddl- referenceTransact sql data definition language - ddl- reference
Transact sql data definition language - ddl- reference
 
SQL Server 2008 for .NET Developers
SQL Server 2008 for .NET DevelopersSQL Server 2008 for .NET Developers
SQL Server 2008 for .NET Developers
 
Module07
Module07Module07
Module07
 
Multi-thematic spatial databases
Multi-thematic spatial databasesMulti-thematic spatial databases
Multi-thematic spatial databases
 
Module01
Module01Module01
Module01
 
Sql server ___________session3-normailzation
Sql server  ___________session3-normailzationSql server  ___________session3-normailzation
Sql server ___________session3-normailzation
 
Alasql - база данных SQL на JavaScript (MoscowJS)
Alasql - база данных SQL на JavaScript (MoscowJS)Alasql - база данных SQL на JavaScript (MoscowJS)
Alasql - база данных SQL на JavaScript (MoscowJS)
 
X query language reference
X query language referenceX query language reference
X query language reference
 
Css introduction
Css introductionCss introduction
Css introduction
 
AlaSQL библиотека для обработки JavaScript данных (презентация для ForntEnd 2...
AlaSQL библиотека для обработки JavaScript данных (презентация для ForntEnd 2...AlaSQL библиотека для обработки JavaScript данных (презентация для ForntEnd 2...
AlaSQL библиотека для обработки JavaScript данных (презентация для ForntEnd 2...
 
5 tsssisu sql_server_2012
5 tsssisu sql_server_20125 tsssisu sql_server_2012
5 tsssisu sql_server_2012
 
SQL Server 2008 for Developers
SQL Server 2008 for DevelopersSQL Server 2008 for Developers
SQL Server 2008 for Developers
 
SQL Server 2008 Spatial Data - Getting Started
SQL Server 2008 Spatial Data - Getting StartedSQL Server 2008 Spatial Data - Getting Started
SQL Server 2008 Spatial Data - Getting Started
 
Module05
Module05Module05
Module05
 
Module02
Module02Module02
Module02
 
Sql Server Data Tools - Codenamed JUNEAU
Sql Server Data Tools - Codenamed JUNEAUSql Server Data Tools - Codenamed JUNEAU
Sql Server Data Tools - Codenamed JUNEAU
 

Similaire à Module04 (20)

Intro to tsql unit 14
Intro to tsql   unit 14Intro to tsql   unit 14
Intro to tsql unit 14
 
Stored procedures
Stored proceduresStored procedures
Stored procedures
 
Stored procedure,transaction
Stored procedure,transactionStored procedure,transaction
Stored procedure,transaction
 
Ruslan Platonov - Transactions
Ruslan Platonov - TransactionsRuslan Platonov - Transactions
Ruslan Platonov - Transactions
 
Mysqlppt
MysqlpptMysqlppt
Mysqlppt
 
Sql transacation
Sql transacationSql transacation
Sql transacation
 
Unit 3(rdbms)
Unit 3(rdbms)Unit 3(rdbms)
Unit 3(rdbms)
 
Unit 3(rdbms)
Unit 3(rdbms)Unit 3(rdbms)
Unit 3(rdbms)
 
Procedures/functions of rdbms
Procedures/functions of rdbmsProcedures/functions of rdbms
Procedures/functions of rdbms
 
PLSQL
PLSQLPLSQL
PLSQL
 
Dynamic websites lec3
Dynamic websites lec3Dynamic websites lec3
Dynamic websites lec3
 
MS SQLSERVER:Sql Functions And Procedures
MS SQLSERVER:Sql Functions And ProceduresMS SQLSERVER:Sql Functions And Procedures
MS SQLSERVER:Sql Functions And Procedures
 
MS SQL SERVER: Sql Functions And Procedures
MS SQL SERVER: Sql Functions And ProceduresMS SQL SERVER: Sql Functions And Procedures
MS SQL SERVER: Sql Functions And Procedures
 
Oracle: Procedures
Oracle: ProceduresOracle: Procedures
Oracle: Procedures
 
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
 
SQl
SQlSQl
SQl
 
SQL Server Stored procedures
SQL Server Stored proceduresSQL Server Stored procedures
SQL Server Stored procedures
 
Stored procedure
Stored procedureStored procedure
Stored procedure
 
Stored procedure
Stored procedureStored procedure
Stored procedure
 
pl/sql Procedure
pl/sql Procedurepl/sql Procedure
pl/sql Procedure
 

Dernier

SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonJericReyAuditor
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 

Dernier (20)

SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lesson
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 

Module04

  • 1. Introduction to Stored Procedure & Functions Module 4
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14. BuiltIn Variables Returns the maximum number of simultaneous user connections allowed on a Microsoft® SQL Server™. The number returned is not necessarily the number currently configured. @@MAXCONNECTION Returns the name of the language currently in use. @@LANGUAGE Returns the name of the local server running Microsoft® SQL Server™. @@SERVERNAME Returns the number of rows affected by the last statement. @@ROWCOUNT Returns the last-inserted identity value. @@IDENTITY Returns the error number for the last Transact-SQL statement executed. @@ERROR Description Variable
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.

Notes de l'éditeur

  1. Faculty Notes : if Defines conditional execution. … else Defines alternate execution when the if condition is false. case Defines conditional expressions using when…then statements instead of if…else. begin Beginning of a statement block. … end End of a statement block. while Repeat performance of statements while condition is true. break Exit from the end of the next outermost while loop. … continue Restart while loop. declare Declare local variables. goto label Go to label:, a position in a statement block. return Exit unconditionally. waitfor Set delay for command execution. print Print a user-defined message or local variable on user’s screen. raiserror Print a user-defined message or local variable on user’s screen and set a system flag in the global variable @@ error . /* comment */ or -- comment Insert a comment anywhere in a Transact-SQL statement.
  2. Faculty Notes: Example: if exists(select zip from authors where zip='9470') print 'Msg print' else print 'Msg print else'
  3. Faculty Notes : Example: select CASE convert(int, (RAND() * 3)) when 0 then "A" when 1 then "B" else "E" end
  4. Faculty Notes : The begin and end keywords create a block for a series of statements enclosed in it. This is treated as one unit by control of flow constructs. Enclosed statements in begin and end are called a statement block . The syntax of begin...end is: begin statement block end
  5. Faculty Notes: A typical transaction is a catalog merchandise order phoned in by a customer and entered into a computer by a customer representative. The order transaction involves checking an inventory database, confirming that the item is available, placing the order and confirming that the order has been placed and noting the expected time of shipment. If we view this as a single transaction, then all of the steps must be completed before the transaction is successful and the database is actually changed to reflect the new order. If something happens before the transaction is successfully completed, any changes to the database must be documented so that they can be undone. SQL Server transactions are often described as having the ACID properties or "passing the ACID test," where ACID is an acronym for atomic, consistent, isolated, and durable. Transactional adherence to the ACID tenets is commonplace in modern RDBMS and is a prerequisite for ensuring the safety and reliability of data.
  6. Faculty Notes: Review points on slide
  7. Faculty Notes: Review points on slide
  8. Faculty Notes: Review points on slide
  9. Faculty Notes: We can begin a Transaction explicitly using keyword BEGIN TRANS and End Trans and add the statements to be executed. When this transition is executed if any of the statements fail the whole transaction will be rolled back In the Example two statements are executed one after another in a transaction The insert statement is executed and one row is inserted (But not yet committed its still in the Transaction) Assume the Delete Statement fails, as Job id 13 is in use in another child table. The @@ERROR variable checks if there’s any error for the transaction and if found rolls back the whole transaction as a whole i.e. It does not insert the record in the employee table
  10. Faculty Notes: These variables are provided by SQL Server. Explanation of each has been described above.
  11. Faculty Notes: Review points on slide
  12. Faculty Notes: Review points on slide
  13. Faculty Notes: Modular programming You can create the procedure once, store it in the database, and call it any number of times in your program. Stored procedures can be created by a person who specializes in database programming, and they can be modified independently of the program source code. Faster Execution If the operation requires a large amount of Transact-SQL code or is performed repetitively, stored procedures can be faster than batches of Transact-SQL code. They are parsed and optimized when they are created, and an in-memory version of the procedure can be used after the procedure is executed the first time. Transact-SQL statements repeatedly sent from the client each time they run, are compiled and optimized every time they are executed by SQL Server. Network traffic An operation requiring hundreds of lines of Transact-SQL code can be performed through a single statement that executes the code in a procedure, rather than by sending hundreds of lines of code over the network. Security Mechanism Users can be granted permission to execute a stored procedure even if they do not have permission to execute the procedure's statements directly.
  14. Faculty Notes: procedure_name Is the name of the new stored procedure. ; number Is an optional integer used to group procedures of the same name so they can be dropped together with a single DROP PROCEDURE statement. For example, the procedures used with an application called orders may be named orderproc ;1, orderproc ;2, and so on. @ parameter Is a parameter in the procedure. One or more parameters can be declared in a CREATE PROCEDURE statement. The value of each declared parameter must be supplied by the user when the procedure is executed (unless a default for the parameter is defined). Specify a parameter name using an at sign (@) as the first character. data_type Is the parameter data type. All data types, including text, ntext and image , can be used as a parameter for a stored procedure. Note   There is no limit on the maximum number of output parameters that can be of cursor data type. VARYING Specifies the result set supported as an output parameter (constructed dynamically by the stored procedure and whose contents can vary). Default Is a default value for the parameter. If a default is defined, the procedure can be executed without specifying a value for that parameter. n Is a placeholder indicating that a maximum of 2,100 parameters can be specified. {RECOMPILE | ENCRYPTION | RECOMPILE , ENCRYPTION} RECOMPILE indicates that SQL Server does not cache a plan for this procedure and the procedure is recompiled at run time. ENCRYPTION indicates that SQL Server encrypts the syscomments table entry containing the text of the CREATE PROCEDURE statement. FOR REPLICATION Specifies that stored procedures created for replication cannot be executed on the Subscriber. A stored procedure created with the FOR REPLICATION option is used as a stored procedure filter and only executed during replication. This option cannot be used with the WITH RECOMPILE option. AS Specifies the actions the procedure is to take. sql_statement Is any number and type of Transact-SQL statements to be included in the procedure. Some limitations apply. n Is a placeholder that indicates multiple Transact-SQL statements may be included in this procedure.
  15. Faculty Notes: The above example creates a stored procedure spAuthorDetail it accepts one parameter @AuthId of int data type when the sp is to be executed the user has to pass value to the stored procedure as shown below execute spAuthorDetail 1456 The values passed to the stored procedure are further passed to the select query. Based on the parameter passed the select statement is executed .
  16. Facutly notes: We can also use function to insert data into a table Insert into mytab values (dbo.addition(2,2))
  17. Faculty Notes: Review the key points on the slide. The key points should be used to summarize the content covered throughout this presentation.