Procedures Organized by Farrokh Alemi, Ph.D. Narrated by Yara Alemi

Slides:



Advertisements
Similar presentations
BD05/06 PL/SQL  Introduction  Structure of a block  Variables and types  Accessing the database  Control flow  Cursors  Exceptions  Procedures.
Advertisements

AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
PL/SQL. Introduction to PL/SQL PL/SQL is the procedure extension to Oracle SQL. It is used to access an Oracle database from various environments (e.g.
PL/SQL.
1 Copyright © 2004, Oracle. All rights reserved. Creating Stored Procedures.
Introduction to Python
Advanced SQL: Stored Procedures Instructor: Mohamed Eltabakh 1.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Basic Elements of C++ Chapter 2.
Introduction to PL/SQL Chapter 9. Objectives Explain the need for PL/SQL Explain the benefits of PL/SQL Identify the different types of PL/SQL blocks.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Stored Procedures Functions Packages
Lecture 4 PL/SQL language. PL/SQL – procedural SQL Allows combining procedural and SQL code PL/SQL code is compiled, including SQL commands PL/SQL code.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Chapter 6: User-Defined Functions
INTRODUCTION TO PL/SQL. Class Agenda Introduction Introduction to PL/SQL Declaring PL/SQL Variable Creating the Executable Section Interacting with the.
Overview · What is PL/SQL · Advantages of PL/SQL · Basic Structure of a PL/SQL Block · Procedure · Function · Anonymous Block · Types of Block · Declaring.
Copyright © Curt Hill Stored Procedures In Transact-SQL.
Advanced SQL: Cursors & Stored Procedures
PL / SQL By Mohammed Baihan. What is PL/SQL? PL/SQL stands for Procedural Language extension of SQL. PL/SQL is a combination of SQL along with the procedural.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
Chapter 9: Advanced SQL and PL/SQL Guide to Oracle 10g.
Variables and control statements in PL\SQL Chapter 10.
Slide 1 of 19Session 13 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 In this session, you will learn to: Implement stored procedures Implement.
CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
PRACTICE OVERVIEW PL/SQL Part Your stored procedure, GET_BUDGET, has a logic problem and must be modified. The script that contains the procedure.
Stored Procedures / Session 4/ 1 of 41 Session 4 Module 7: Introducing stored procedures Module 8: More about stored procedures.
Oracle10g Developer: PL/SQL Programming1 Objectives Named program units How to identify parameters The CREATE PROCEDURE statement Creating a procedure.
Creating Procedures. PL/SQL Program Construct Tools Constructs Anonymous Block Application procedures or functions Application packages Application Triggers.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
Advanced SQL: Cursors & Stored Procedures Instructor: Mohamed Eltabakh 1.
Kingdom of Saudi Arabia Ministry of Higher Education Al-Imam Muhammad Ibn Saud Islamic University College of Computer and Information Sciences Overview.
Stored Procedures and Functions Pemrograman Basis Data MI2183.
Chapter # 2 Part 2 Programs And data
Chapter Topics The Basics of a C++ Program Data Types
User-Written Functions
Chapter 6: User-Defined Functions I
Oracle11g: PL/SQL Programming Chapter 5 Procedures.
Creating Stored Procedures and Functions
Yanal Alahmad Java Workshop Yanal Alahmad
PL/SQL.
Basic Elements of C++.
UNIT - V STORED PROCEDURE.
The Selection Structure
Variables, Expressions, and IO
Functions CIS 40 – Introduction to Programming in Python
User-Defined Functions
PL/SQL Scripting in Oracle:
Dead Man Visiting Farrokh Alemi, PhD Narrated by …
Basic Elements of C++ Chapter 2.
Arrays, For loop While loop Do while loop
SQL Text Manipulation Farrokh Alemi, Ph.D.
Chapter 4 void Functions
GROUP BY & Subset Data Analysis
SQL for Calculating Likelihood Ratios
SELECT & FROM Commands Farrokh Alemi, PhD
Rank Order Function Farrokh Alemi, Ph.D.
Creating Tables & Inserting Values Using SQL
Cursors Organized by Farrokh Alemi, Ph.D. Narrated by Yara Alemi
Convert from Variable Character to Float
Chapter # 2 Part 2 Programs And data
Indexing & Computational Efficiency
Chapter 6: User-Defined Functions I
PL/SQL Declaring Variables.
Procedures Oracle & MySQL
Presentation transcript:

Procedures Organized by Farrokh Alemi, Ph.D. Narrated by Yara Alemi This section provides a brief introduction to receiver Operating Curves, a tool used to test the predictive accuracy of models. This brief presentation was organized by Dr. Alemi.

Procedure Syntax Again Procedures are used when portion of a code must be repeatedly executed. A procedure is a subprogram or module that performs a particular data manipulation task. Procedures allow ‘modular design‘ of the SQL code. A subprogram can be called repeatedly within the code thus reducing repeated code.

CREATE PROCEDURE DROP PROCEDURE Procedures are created with the CREATE PROCEDURE statement. It is stored in the database and can be deleted with the DROP PROCEDURE statement.

Declarative Executable Exceptions Each procedure has a name, and may also have a parameter list. It also has the following three parts: declarative, executable, and exceptions. Declarative Part is an optional part. It contains declarations of types, cursors, constants, variables, exceptions, and nested subprograms. These items are local to the procedure and cease to exist when it completes execution.

Declarative Executable Exceptions The executable Part is a mandatory part and contains statements that perform the designated action.

Declarative Executable Exceptions The Exception-handling part is an optional part. It contains the code that handles errors during run time.

CREATE [OR REPLACE] PROCEDURE procedure_name [(parameter_name [IN | OUT | IN OUT] type [, ...])] {IS | AS} BEGIN < procedure_body > END; A procedure is created or replaced with the CREATE statement.

CREATE [OR REPLACE] PROCEDURE procedure_name [(parameter_name [IN | OUT | IN OUT] type [, ...])] {IS | AS} BEGIN < procedure_body > END; The procedure-name obviously specifies the name of the procedure. It must not be a reserved word and should not contain space.

CREATE [OR REPLACE] PROCEDURE procedure_name [(parameter_name [IN | OUT | IN OUT] type [, ...])] {IS | AS} BEGIN < procedure_body > END; The optional parameter list contains name, mode and types of the parameters. IN represents the value that will be passed from outside, when the procedure is called. It is a read-only parameter. Inside the subprogram, an IN parameter acts like a constant. It cannot be assigned a value. You can pass a constant, literal, initialized variable, or expression as an IN parameter. 

CREATE [OR REPLACE] PROCEDURE procedure_name [(parameter_name [IN | OUT | IN OUT] type [, ...])] {IS | AS} BEGIN < procedure_body > END; OUT represents the parameter that will be used to return a value to the calling program. Inside the procedure, an OUT parameter acts like a variable. You can change its value and reference the value after assigning it.

CREATE [OR REPLACE] PROCEDURE procedure_name [(parameter_name [IN | OUT | IN OUT] type [, ...])] {IS | AS} BEGIN < procedure_body > END; An IN OUT parameter type passes an initial value to a subprogram and returns an updated value to the caller. It can be assigned a value in the calling program and the value can be updated in the procedure.

CREATE [OR REPLACE] PROCEDURE procedure_name [(parameter_name [IN | OUT | IN OUT] type [, ...])] {IS | AS} BEGIN < procedure_body > END; Type refers to variable type such as integer, float, variable character and so on.

CREATE [OR REPLACE] PROCEDURE procedure_name [(parameter_name [IN | OUT | IN OUT] type [, ...])] {IS | AS} BEGIN < procedure_body > END; The AS keyword is used instead of the IS keyword for creating a standalone procedure. Within a calling program, the EXECUTE command runs the standalone procedure.

CREATE [OR REPLACE] PROCEDURE procedure_name [(parameter_name [IN | OUT | IN OUT] type [, ...])] {IS | AS} BEGIN < procedure_body > END; procedure-body contains the executable part. This is where the input variables are modified to produce the output variables

CREATE [OR REPLACE] PROCEDURE procedure_name [(parameter_name [IN | OUT | IN OUT] type [, ...])] {IS | AS} BEGIN < procedure_body > END; The last line of a procedure should be the END command.

Example Procedure Again We demonstrate the use of procedure with one created to calculate likelihood ratio.

-- DROP PROCEDURE Likelihood_Ratio; CREATE PROCEDURE Likelihood_Ratio (@DX varchar(7), @Alive float, @Dead float ) AS BEGIN SELECT MAX(CASE WHEN Cast(ICD9 as varchar(7))=@Dx then 1. Else 0. END) AS Dx , MAX(CASE WHEN AgeAtDeath is not null then 1. Else 0. END) AS Dead INTO #temp1 FROM [AgeDx].[dbo].[final] GROUP BY id CASE WHEN SUM(Dead)is null then 1/SUM(1-Dead) WHEN SUM(1-Dead)<1 then SUM(Dead)+1 Else (SUM(Dx*Dead)/MAX(@Dead))/(SUM(Dx*(1-Dead))/MAX(@Alive)) END AS [Likelihood Ratio] FROM #temp1 END In this procedure we calculate the likelihood ratio associated with a specific diagnosis.

-- DROP PROCEDURE Likelihood_Ratio; CREATE PROCEDURE Likelihood_Ratio (@DX varchar(7), @Alive float, @Dead float ) AS BEGIN SELECT MAX(CASE WHEN Cast(ICD9 as varchar(7))=@Dx then 1. Else 0. END) AS Dx , MAX(CASE WHEN AgeAtDeath is not null then 1. Else 0. END) AS Dead INTO #temp1 FROM [AgeDx].[dbo].[final] GROUP BY id CASE WHEN SUM(Dead)is null then 1/SUM(1-Dead) WHEN SUM(1-Dead)<1 then SUM(Dead)+1 Else (SUM(Dx*Dead)/MAX(@Dead))/(SUM(Dx*(1-Dead))/MAX(@Alive)) END AS [Likelihood Ratio] FROM #temp1 END The first line is commented out and is used only when we wish to drop the previous draft of the procedure from the database.

-- DROP PROCEDURE Likelihood_Ratio; CREATE PROCEDURE Likelihood_Ratio (@DX varchar(7), @Alive float, @Dead float ) AS BEGIN SELECT MAX(CASE WHEN Cast(ICD9 as varchar(7))=@Dx then 1. Else 0. END) AS Dx , MAX(CASE WHEN AgeAtDeath is not null then 1. Else 0. END) AS Dead INTO #temp1 FROM [AgeDx].[dbo].[final] GROUP BY id CASE WHEN SUM(Dead)is null then 1/SUM(1-Dead) WHEN SUM(1-Dead)<1 then SUM(Dead)+1 Else (SUM(Dx*Dead)/MAX(@Dead))/(SUM(Dx*(1-Dead))/MAX(@Alive)) END AS [Likelihood Ratio] FROM #temp1 END This line gives the name of the procedure and the types of variables that are used as input in the procedure. Note that the name of these variables could be different from the names used to call the procedure and the computer will set the calling variable set to the procedure variable set in the order listed. The seven letter-long variable called at DX contains the diagnosis code. The procedure will calculate the likelihood ratio associated with this diagnosis code.

-- DROP PROCEDURE Likelihood_Ratio; CREATE PROCEDURE Likelihood_Ratio (@DX varchar(7), @Alive float, @Dead float ) AS BEGIN SELECT MAX(CASE WHEN Cast(ICD9 as varchar(7))=@Dx then 1. Else 0. END) AS Dx , MAX(CASE WHEN AgeAtDeath is not null then 1. Else 0. END) AS Dead INTO #temp1 FROM [AgeDx].[dbo].[final] GROUP BY id CASE WHEN SUM(Dead)is null then 1/SUM(1-Dead) WHEN SUM(1-Dead)<1 then SUM(Dead)+1 Else (SUM(Dx*Dead)/MAX(@Dead))/(SUM(Dx*(1-Dead))/MAX(@Alive)) END AS [Likelihood Ratio] FROM #temp1 END This section is the execution code. During this section computer calculates a likelihood ratio for the diagnoses code used to call the procedure.

-- DROP PROCEDURE Likelihood_Ratio; CREATE PROCEDURE Likelihood_Ratio (@DX varchar(7), @Alive float, @Dead float ) AS BEGIN SELECT MAX(CASE WHEN Cast(ICD9 as varchar(7))=@Dx then 1. Else 0. END) AS Dx , MAX(CASE WHEN AgeAtDeath is not null then 1. Else 0. END) AS Dead INTO #temp1 FROM [AgeDx].[dbo].[final] GROUP BY id CASE WHEN SUM(Dead)is null then 1/SUM(1-Dead) WHEN SUM(1-Dead)<1 then SUM(Dead)+1 Else (SUM(Dx*Dead)/MAX(@Dead))/(SUM(Dx*(1-Dead))/MAX(@Alive)) END AS [Likelihood Ratio] FROM #temp1 END It reads the data from premanant table “final” in “agedx” database. For each unique person, it calculates if the patient has the diagnosis or does not. It also calculates if the patient is dead or alive. It puts these data in temporary file 1. Notice that repeated calls to the procedure does not require us to drop the temporary file as each call causes all temporary files to drop.

-- DROP PROCEDURE Likelihood_Ratio; CREATE PROCEDURE Likelihood_Ratio (@DX varchar(7), @Alive float, @Dead float ) AS BEGIN SELECT MAX(CASE WHEN Cast(ICD9 as varchar(7))=@Dx then 1. Else 0. END) AS Dx , MAX(CASE WHEN AgeAtDeath is not null then 1. Else 0. END) AS Dead INTO #temp1 FROM [AgeDx].[dbo].[final] GROUP BY id CASE WHEN SUM(Dead)is null then 1/SUM(1-Dead) WHEN SUM(1-Dead)<1 then SUM(Dead)+1 Else (SUM(Dx*Dead)/MAX(@Dead))/(SUM(Dx*(1-Dead))/MAX(@Alive)) END AS [Likelihood Ratio] FROM #temp1 END Next the likelihood ratio is calculated for patients who have the diagnosis. Separate calculations are done for situations where no one with the diagnosis is dead, where everyone with the diagnosis is dead, and where some of the patients with the diagnosis are dead and some are alive.

Execution of Procedure Again To run a procedure one must call it using the EXECUTE command

EXECUTE procedure_name [parameter_name [, ...]] The syntax is the reserve word execute followed by the procedure name followed with procedure parameters separated by commas.

DECLARE @Diag varchar(7), @TotalAlive float, @TotalDead float Set @Diag = 'I789.00' Set @TotalAlive = (SELECT CAST(COUNT(distinct ID) as FLOAT) FROM [AgeDx].[dbo].[final] WHERE AgeAtDeath is null) Set @TotalDead = (SELECT CAST(COUNT(distinct ID) as FLOAT) FROM [AgeDx].[dbo].[final] WHERE AgeAtDeath is not null) Execute likelihood_Ratio @diag, @TotalAlive, @TotalDead This code calls the previously described likelihood ratio procedure.

DECLARE @Diag varchar(7), @TotalAlive float, @TotalDead float Set @Diag = 'I789.00' Set @TotalAlive = (SELECT CAST(COUNT(distinct ID) as FLOAT) FROM [AgeDx].[dbo].[final] WHERE AgeAtDeath is null) Set @TotalDead = (SELECT CAST(COUNT(distinct ID) as FLOAT) FROM [AgeDx].[dbo].[final] WHERE AgeAtDeath is not null) Execute likelihood_Ratio @diag, @TotalAlive, @TotalDead First the various parameters are declared and calculated. Here the diagnosis code is set and the total patients alive and dead are calculated from the data.

DECLARE @Diag varchar(7), @TotalAlive float, @TotalDead float Set @Diag = 'I789.00' Set @TotalAlive = (SELECT CAST(COUNT(distinct ID) as FLOAT) FROM [AgeDx].[dbo].[final] WHERE AgeAtDeath is null) Set @TotalDead = (SELECT CAST(COUNT(distinct ID) as FLOAT) FROM [AgeDx].[dbo].[final] WHERE AgeAtDeath is not null) Execute likelihood_Ratio @diag, @TotalAlive, @TotalDead The execute command tells the computer to run the procedure called likelihood ratio and to pass to this procedure three pieces of data, a constant containing the diagnosis code in text, and two other floating numbers containing the total number of patients alive or dead.

The results of the executing the procedure is the calculated likelihood ratio that can be passed back to the calling program or stored in a permanent file using the APPEND command.

One can repeatedly call and execute a procedure One can repeatedly call and execute a procedure. This provides an important advantage to coding as it allows portion of the code to be reused.

Procedures can stream line repeated code