G. Green 1.  Options include:  Script Files  already covered  APIs  last course topic  Database-Stored Code  our focus 2.

Slides:



Advertisements
Similar presentations
Transact-SQL. 1. Declare float = 10 select * from customers where discnt
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.
Chapter 4 5 6_ SQL SQL Is: Structured Query Language
Chapter 4B: More Advanced PL/SQL Programming
© 2007 by Prentice Hall 1 Chapter 8: Advanced SQL Modern Database Management 8 th Edition Jeffrey A. Hoffer, Mary B. Prescott, Fred R. McFadden.
Module 9: Implementing Stored Procedures. Introduction to Stored Procedures Creating Executing Modifying Dropping Using Parameters in Stored Procedures.
Fundamentals, Design, and Implementation, 9/e Chapter 11 Managing Databases with SQL Server 2000.
IS 4420 Database Fundamentals Chapter 8: Advanced SQL Leon Chen
1 IS 4420 Database Fundamentals Chapter 7: Introduction to SQL Leon Chen.
SQL Basics. SQL SQL (Structured Query Language) is a special-purpose programming language designed from managing data in relational database management.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-1 David M. Kroenke’s Chapter Seven: SQL for Database Construction and.
IMS 4212: Application Architecture and Intro to Stored Procedures 1 Dr. Lawrence West, Management Dept., University of Central Florida
Dinamic SQL & Cursor. Why Dinamic SQL ? Sometimes there is a need to dynamically create a SQL statement on the fly and then run that command. This can.
Stored Procedures, Transactions, and Error-Handling
Module 1: Introduction to Transact-SQL
1 Chapter 8: Advanced SQL. Chapter 8 2 Processing Multiple Tables – Joins Join – a relational operation that causes two or more tables with a common domain.
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi.
T-SQL Transact-SQL is microsoft implementation of SQL. It contains additional programming constracts T-SQL enables you to write programs that contain SQL.
Advanced SQL: Cursors & Stored Procedures
1 Chapter 8: Advanced SQL Modern Database Management Jeffrey A. Hoffer, Mary B. Prescott, Fred R. McFadden.
1 © Prentice Hall, 2002 Chapter 8: Advanced SQL Modern Database Management 6 th Edition Jeffrey A. Hoffer, Mary B. Prescott, Fred R. McFadden.
Modern Database Management Chapter 8: Advanced SQL.
© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1 Chapter 7 (Part a): Introduction to SQL Modern Database Management 9 th Edition Jeffrey A.
Lec 3- B Database Integrity 1. Overview Define a database using SQL data definition language Work with Views Write single table queries Establish referential.
Commercial RDBMSs Access and Oracle. Access DBMS Architchecture  Can be used as a standalone system on a single PC: -JET Engine -Microsoft Data Engine.
Objectives Database triggers and syntax
06 | Modifying Data in SQL Server Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program Manager.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
A Guide to SQL, Eighth Edition Chapter Eight SQL Functions and Procedures.
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 9 Database Triggers.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
SQL Overview Structured Query Language
Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha Function, Trigger used in PosgreSQL.
1 Intro stored procedures Declaring parameters Using in a sproc Intro to transactions Concurrency control & recovery States of transactions Desirable.
Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha Views, Sequence, and Stored Procedure used in PosgreSQL.
Learningcomputer.com SQL Server 2008 –Views, Functions and Stored Procedures.
Copyright © 2013 Curt Hill Triggers The Generation of Indirect Actions.
A procedure is a module performing one or more actions; it does not need to return any values. The syntax for creating a procedure is as follows: CREATE.
PL/SQL programming Procedures and Cursors Lecture 1 [Part 2]
IS6146 Databases for Management Information Systems Lecture 4: SQL IV – SQL Functions and Procedures Rob Gleasure robgleasure.com.
SQL Server 2012 Session: 1 Session: 12 Triggers Data Management Using Microsoft SQL Server.
PRACTICE OVERVIEW PL/SQL Part Your stored procedure, GET_BUDGET, has a logic problem and must be modified. The script that contains the procedure.
IMS 4212: Application Architecture and Intro to Stored Procedures 1 Dr. Lawrence West, Management Dept., University of Central Florida
IMS 4212: Constraints & Triggers 1 Dr. Lawrence West, Management Dept., University of Central Florida Stored Procedures in SQL Server.
IT420: Database Management and Organization Triggers and Stored Procedures 24 February 2006 Adina Crăiniceanu
Module 9: Using Advanced Techniques. Considerations for Querying Data Working with Data Types Cursors and Set-Based Queries Dynamic SQL Maintaining Query.
1 Database Fundamentals Introduction to SQL. 2 SQL Overview Structured Query Language The standard for relational database management systems (RDBMS)
SQL Triggers, Functions & Stored Procedures Programming Operations.
1 Stored Procedure, Function and Trigger. 2Objectives 1. Database Programming 2. Stored Procedure 3. Function 4. Trigger.
CS422 Principles of Database Systems Stored Procedures and Triggers Chengyu Sun California State University, Los Angeles.
In this session, you will learn to: Create and manage views Implement a full-text search Implement batches Objectives.
1. Advanced SQL Functions Procedural Constructs Triggers.
Special Topics in Database. DATABASE-STORED CODE.
Chapter 8 1 Lecture Advanced SQL. Chapter 8 2 Processing Multiple Tables–Joins Join – a relational operation that causes two or more tables with.
1 Copyright © 2004, Oracle. All rights reserved. PL/SQL Programming Concepts: Review.
SQL Query Getting to the data ……..
Rob Gleasure robgleasure.com
SQL and SQL*Plus Interaction
PROCEDURES, CONDITIONAL LOGIC, EXCEPTION HANDLING, TRIGGERS
Modern Database Management Jeffrey A. Hoffer, Mary B. Prescott,
CHAPTER 7: ADVANCED SQL.
CHAPTER 6: INTRODUCTION TO SQL
Chapter 7 Using SQL in Applications
Oracle9i Developer: PL/SQL Programming Chapter 8 Database Triggers.
Chapter 11 Managing Databases with SQL Server 2000
COP 2700 – Data Structures (SQL)
Presentation transcript:

G. Green 1

 Options include:  Script Files  already covered  APIs  last course topic  Database-Stored Code  our focus 2

 T-SQL Basics  Cursors  Parameters procedure-with-parameters/  Good Practices  look at this one! practices-checklist.aspx 3

 Program code stored in database T-SQL CLR Languages (C#, VB.net, …)  Good for: sharing code performance Enforcing security, business rules, audits 4 Procedure A SQL… SQL end A Application Program execute A execute B Database

Stored Procedure T-SQL block Procedural programming features stored in database * invoked by name * accepts arguments Database Trigger T-SQL block Procedural programming features stored in database * invoked by DML (Insert, Update, or Delete) * accepts no arguments 5

6 CREATE PROCEDURE [parameter_information] AS DECLARE /* program variables defined here */ BEGIN /* program logic goes here */ END

7 /* retrieves and displays all customer records */ IF EXISTS (SELECT NAME FROM SYSOBJECTS WHERE NAME = 'show_all_customers' AND TYPE = 'P') DROP PROCEDURE show_all_customers; GO CREATE PROCEDURE show_all_customers AS BEGIN SELECT customer_id, customer_name, postal_code FROM customer_t; END;

8 execute show_all_customers; -- OR Exec show_all_customers;

9 /* retrieves a specific customer record based on a given customer ID. */ /* if the customer lives in the state of FL, then append “-1111” after the zip code & print a message to user */ /* CONVERTs needed since we're concatenating numeric data to text data */ IF EXISTS (SELECT NAME FROM SYSOBJECTS WHERE NAME = 'alter_zip_and_print' AND TYPE = 'P') DROP PROCEDURE alter_zip_and_print; GO CREATE PROCEDURE numeric(5) = null AS varchar(12); BEGIN = = = state FROM customer_t WHERE customer_id = ' fl ' BEGIN + ' ' UPDATE customer_t SET postal_code WHERE customer_id print 'cust#: ' + zipcode changed to: ' END; GO

10 /* statement below included ONLY so I can undo changes after demo. You DO NOT need to do this */ Set implicit_transactions on GO /* show customer 9's record before calling procedure */ Select * from customer_t where customer_id = 9; exec = 9; -or- exec alter_zip_and_print 9 GO /* check to see if procedure updated customer 9's record */ Select * from customer_t where customer_id = 9; GO /* statement below included ONLY so I can undo changes after demo. You DO NOT need to do this */ Rollback; Set implicit_transactions off

Require cursorscursors 1. Declare CURSOR 2. OPEN cursor 3. FETCH data from cursor 4. CLOSE & DEALLOCATE cursor Use control structures to process each record WHILE … [BREAK can be used to force exit from WHILE loop] END; IF ELSE IF BEGIN END; ELSE BEGIN END; 11

12 /* retrieve customer records based on NAME; if customer(s) live in FL, ‘-1111’ is appended to zip */ IF EXISTS (SELECT NAME FROM SYSOBJECTS WHERE NAME = 'getcust' AND TYPE = 'P') DROP PROCEDURE getcust; GO CREATE PROCEDURE varchar(25) AS DECLARE custcursor CURSOR FOR SELECT customer_id, state, postal_code from customer_t WHERE customer_name like ' % ' + ' % ' ; numeric varchar(12); BEGIN -- logic OPEN custcursor; --find customers meeting criteria IF = 0 BEGIN RAISERROR ( ' no customer found ', 10,1) RETURN; END; FETCH FROM @pcode; /* code continued here… */ WHILE = 0 BEGIN -- loop thru each customer record found = ' fl ' BEGIN -- process and display old & new zip PRINT ' id: ' + + ', name: ' PRINT ' old zip: ' SET + ' ' UPDATE customer_t SET postal_code WHERE customer_id PRINT ' new zip: ' PRINT ' ======================= ' ; END; -- processing and displaying of zip FETCH NEXT FROM @pcode; END; -- looping thru records CLOSE custcursor; DEALLOCATE custcursor; END; -- logic

13 Set implicit_transactions on GO Update customer_t set state= ' fl ' where customer_id=2; Select * from customer_t where customer_name like ' %furn% ' ; Exec getcust ' furniture ' GO Select * from customer_t where customer_name like ' %furn% ' ; GO Rollback; Set implicit_transactions off

14 /* for a given order, show the total number of products on the order, and the description of each product on the order */ IF EXISTS (SELECT NAME FROM SYSOBJECTS WHERE NAME = 'showordereditems' AND TYPE = 'P') DROP PROCEDURE showordereditems; GO CREATE PROCEDURE numeric(11) AS DECLARE ordercursor CURSOR FOR select ol.order_id, ol.product_id, p.product_description from order_line_t ol, product_t p where ol.product_id = p.product_id and ol.order_id numeric(11), -- storing order numeric(11), -- storing product varchar(50), -- storing product numeric(4); -- to store the number of products on order BEGIN -- logic -- initialize the counter = count(*) FROM order_line_t ol WHERE ol.order_id -- calculate total # of products on given order PRINT '** order# ' + + ' has ' + + ' products:**'; -- print the total # of products on the order OPEN ordercursor; -- find the products associated with the given order IF BEGIN RAISERROR ('order not found',10,1) RETURN; END; FETCH FROM @pdesc; WHILE = 0 -- loop thru each product on the order BEGIN PRINT 'product#: ' + convert ) + ' product desc: ' -- print each product on the order FETCH NEXT FROM @pdesc; END; CLOSE ordercursor; DEALLOCATE ordercursor; END; -- logic

15 Exec showordereditems 1002 GO

 Store code that creates procedure in a script  Run and debug script in SSMS  Creates & stores procedure in database  Invoke procedure  Issue "execute" command  Include parameter values where required 16

 Common Uses:  Implementing RI  Complex defaults  Interrelation constraints/updates  Updating views  Auditing business activities  2 Kinds in SQL Server:  After  Instead of (see to-use-instead-of-trigger-guest-post-by-vikas-munjal-koenig- solutions/) to-use-instead-of-trigger-guest-post-by-vikas-munjal-koenig- solutions/ 17

18 CREATE TRIGGER ON AS DECLARE /* program variables defined here */ BEGIN /* program logic goes here */ END

19 /* maintain the orders placed counter in the customer table */ /* add 1 to the counter each time a customer places a new order */ IF EXISTS (SELECT name FROM sysobjects WHERE name = 'add_order_placed' AND type = 'TR') DROP TRIGGER add_order_placed; GO CREATE TRIGGER add_order_placed ON order_t AFTER INSERT AS AS numeric(11,0) BEGIN = customer_ID FROM INSERTED; UPDATE customer_t SET orders_placed = orders_placed+1 WHERE customer_t.customer_ID END; GO

20 /* Maintain the orders placed counter in the customer table */ /* Every time the customer# on an order is changed, the orders_placed column in the customer table is updated */ /* to reflect the # of orders placed by both the original and new customer */ IF EXISTS (SELECT name FROM sysobjects WHERE name = 'change_order_placed' AND type = 'TR') DROP TRIGGER change_order_placed; GO CREATE TRIGGER change_order_placed ON order_t AFTER UPDATE AS AS AS numeric(11,0) BEGIN = customer_ID FROM INSERTED; = customer_ID FROM DELETED; UPDATE customer_t SET orders_placed = orders_placed+1 WHERE customer_t.customer_ID UPDATE customer_t SET orders_placed = orders_placed-1 WHERE customer_t.customer_ID END; GO

Set implicit_transactions on GO /* check status of customer’s orders_placed counter before placing new orders */ select customer_id, orders_placed from customer_t where customer_id=10; /* fire insert trigger by placing orders for customer #10 */ insert into order_t values (9900, '10-OCT-01', 10); insert into order_t values (9901, '10-OCT-01', 10); insert into order_t values (9902, '10-OCT-01', 10); /* issue select statement to verify that trigger updated orders_placed counter */ select customer_id, orders_placed from customer_t where customer_id=10; /* select status of customer’s orders_placed counters before placing new orders */ select customer_id, orders_placed from customer_t where customer_id=10; select customer_id, orders_placed from customer_t where customer_id=1; /* fire update trigger by changing the customer# of one of the orders for customer #10 */ update order_t set customer_id = 1 where order_id = 9901; /* issue select statement to verify that the orders_placed counter is correct */ select customer_id, orders_placed from customer_t where customer_id=10; select customer_id, orders_placed from customer_t where customer_id=1; GO Rollback; Set implicit_transactions off

/* when a new product is added, if the standard price is NOT given, then set the product’s standard price to either: */ /* the highest standard price of a product in the same product line if one exists, OR to $300 */ IF EXISTS (SELECT name FROM sysobjects WHERE name = 'set_standard_price' AND type = 'TR') DROP TRIGGER set_standard_price; GO CREATE TRIGGER set_standard_price ON product_t AFTER INSERT AS as as as as numeric (11,0) = = standard_price FROM INSERTED; IS NULL BEGIN = MAX(standard_price) FROM product_t WHERE product_line_id IS NOT NULL ELSE = 300; UPDATE product_t SET standard_price WHERE product_id END --end IF END; -- end program GO 22

Set implicit_transactions on GO /* issue select statement to view product table before adding new products */ select * from product_t; /* fire insert trigger adding new products to the product_t table*/ insert into product_t values (999,null,null,null,null,20001); insert into product_t values (998, null, null, 123, 3, 33333); Insert into product_t values (997,null,null,null,null,44444); /* issue select statement to verify that trigger updated standard prices for products */ select product_id, product_line_id, standard_price from product_t where product_id>900; GO Rollback; Set implicit_transactions off 23