Task oriented processing

Slides:



Advertisements
Similar presentations
Chapter 8 Embedded SQL.
Advertisements

Let’s try Oracle. Accessing Oracle The Oracle system, like the SQL Server system, is client / server. For SQL Server, –the client is the Query Analyser.
Chapter 4B: More Advanced PL/SQL Programming
Populating and Querying tables Insert and mostly View (DML)
PL/SQL Continued. So far… Anonymous blocks Procedures Have you tried… –Downloading and running the dispcust.sql sample from your web page? –Writing your.
Specification for Write Docket From Joe’s Yard. Requirements We’ve seen the docket and know it needs to be filled. To fill the docket, we need to: –Generate.
Program design example Task: Develop an algorithm expressed in pseudocode for a specified problem specified problem.
A Guide to SQL, Seventh Edition. Objectives Embed SQL commands in PL/SQL programs Retrieve single rows using embedded SQL Update a table using embedded.
Stored Procedures Dr. Ralph D. Westfall May, 2009.
Overview What is SQL Server? Creating databases Administration Security Backup.
Copyright © 2010 Pearson Education, Inc. Publishing as Prentice Hall 1 1. Chapter 2: Relational Databases and Multi-Table Queries Exploring Microsoft Office.
IMS 4212: Application Architecture and Intro to Stored Procedures 1 Dr. Lawrence West, Management Dept., University of Central Florida
Introduction to Databases Chapter 7: Data Access and Manipulation.
ADO.NET A2 Teacher Up skilling LECTURE 3. What’s to come today? ADO.NET What is ADO.NET? ADO.NET Objects SqlConnection SqlCommand SqlDataReader DataSet.
Copyright © 2007, Oracle. All rights reserved. Managing Concurrent Requests.
LiveCycle Data Services Introduction Part 2. Part 2? This is the second in our series on LiveCycle Data Services. If you missed our first presentation,
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.
Chapter 12: How Long Can This Go On?
Module 1: Introduction to Transact-SQL
T-SQL Transact-SQL is microsoft implementation of SQL. It contains additional programming constracts T-SQL enables you to write programs that contain SQL.
SYST Web Technologies SYST Web Technologies Databases & MySQL.
Oracle’s take on joins Where it differs from ANSI standard.
IMS 4212: Intro to SQL 1 Dr. Lawrence West, Management Dept., University of Central Florida Introduction to SQL—Topics Introduction to.
Copyright © Curt Hill Stored Procedures In Transact-SQL.
Stored Procedure. Objective At the end of the session you will be able to know :  What are Stored Procedures?  Create a Stored Procedure  Execute a.
Module 8: Implementing Stored Procedures. Overview Implementing Stored Procedures Creating Parameterized Stored Procedures Working With Execution Plans.
CIS4368: Advanced DatabaseSlide # 1 PL/SQL Dr. Peeter KirsSpring, 2003 PL/SQL.
Guide to Oracle 10g ITBIS373 Database Development Lecture 4a - Chapter 4: Using SQL Queries to Insert, Update, Delete, and View Data.
1 Chapter 4: Creating Simple Queries 4.1 Introduction to the Query Task 4.2 Selecting Columns and Filtering Rows 4.3 Creating New Columns with an Expression.
Chapter Fourteen Access Databases and SQL Programming with Microsoft Visual Basic th Edition.
Views, Algebra Temporary Tables. Definition of a view A view is a virtual table which does not physically hold data but instead acts like a window into.
A Guide to SQL, Eighth Edition Chapter Eight SQL Functions and Procedures.
Enterprise manager Using the Enterprise manager. Purpose of the Enterprise Manager To design tables To populate / update tables To draw diagrams of tables.
Learningcomputer.com SQL Server 2008 –Views, Functions and Stored Procedures.
31/01/ Selection If selection construct.
Module Road Map The Scope of the Problem A range of potential problems Lost Updates User A reads a record User B reads the same record User A makes changes.
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.
SQL Triggers, Functions & Stored Procedures Programming Operations.
Programming with Microsoft Visual Basic 2012 Chapter 14: Access Databases and SQL.
Dynamic SQL Writing Efficient Queries on the Fly ED POLLACK AUTOTASK CORPORATION DATABASE OPTIMIZATION ENGINEER.
1. Advanced SQL Functions Procedural Constructs Triggers.
Writing Simple Queries in Access
COMP 430 Intro. to Database Systems
A Guide to SQL, Seventh Edition
Dynamic SQL Writing Efficient Queries on the Fly
PL/pgSQL
Chapter 3: Variables, Functions, Math, and Strings
Requisitions from Stock
Programmability by Adrienne Watt.
PROCEDURES, CONDITIONAL LOGIC, EXCEPTION HANDLING, TRIGGERS
Dynamic SQL Writing Efficient Queries on the Fly
Error Handling Summary of the next few pages: Error Handling Cursors.
JavaScript: Functions.
Lecture 07 More Repetition Richard Gesick.
Microsoft Access Illustrated
For a new user you must click on the “Registration for Generator” link
CPSC-310 Database Systems
Determine Labor and Payroll Reporting Functions
Database Fundamentals
Conditions and Ifs BIS1523 – Lecture 8.
7 Arrays.
Chapter 2 Handling Data in PL/SQL Blocks Oracle9i Developer:
Creating Noninput Items
Computer Science Projects Database Theory / Prototypes
Exploring the Power of EPDM Tasks Working with and Developing Tasks in SolidWorks Enterprise PDM (EPDM) By: Marc Young xLM Solutions
Topic 12 Lesson 2 – Retrieving Data with Queries
COP 2700 – Data Structures (SQL)
CHAPTER 6 Testing and Debugging.
Presentation transcript:

Task oriented processing And Queries

How do I want my information? A customer should not be required to remember An order number A product code An account code except for security purposes

Where is my…? A customer of Northwind wants to know Where is the order I placed last week? How much do I pay for Chai? How much more does Chai cost than Chang? What is the least expensive drink? What is the fanciest (most expensive) cheese?

How do I manage …? A line manager wants to know Are there any products that are out of stock? Are there any customers on the record that have never placed an order? Are there any employees who have never made a sale?

How do I save …? A managing director wants to know What is our best selling product? What is our highest earning product? Who is our highest earning employee? Who is the employee who sells the most units?

Formulating queries Ask the question the user would ask Avoid code-dependent queries – go for name-based or point and pick based queries. Once the query has been stated Consult the ERD Check the enquiry access path Try to fill the query using joins rather than separate queries.

Saving your queries in your database Stored Procedures Saving your queries in your database

Simple Stored Procedures Create the query and check it in the Query Analyser. Using the Enterprise Manager, Add a new stored procedure to your database. Name your Stored Procedure appropriately. Paste in your query, check syntax and click OK. Run your stored procedure from the query analyser using ‘EXEC <SP_NAME>.

Using variables Variables can be used in SQL Server using either the Query Analyser or Stored Procedures. All variables must begin with the symbol @ Variables must be given a data type: e.g. Declare @cost numeric(6,2); Declare @name varchar(20);

Using Variables Variables can be assigned values using Set: Set @cost = 0; Set @cost = @cost * 1.1; Or using Select: Declare @givenId numeric(7,0); Set @givenId = 1234567 Select @name = suppliername from Supplier where SupplierId = @givenId;

Parameters in Stored Procedures Parameters can be passed to stored procedures by reference. If the stored procedure is expected to return a significant value, then it should be explicitly marked as an output parameter. To use parameters: Define the formal parameters in the Stored Procedure. Define the actual parameters and give them values in the Query Analyser.

Exercise Write a stored procedure that will allow Joe to add an orderline to his database, decrementing the stock quantity in the stock table and returning a cost for that orderline. If there is not enough stock in the yard, a message should be displayed and the new orderline should be rejected.

Procedure specification Input: OrderNo, StockCode, QuantityRequired Output: The linecost To reject the request if the stock is not in the yard: Read the stock row Check the quantity If enough, Decrement stock Calculate line cost Update record Write new orderline Else Print error message

CREATE procedure sp_SELLSTOCK @DCKT numeric (5,0), @STKCD varchar(5), @QTYREQ numeric(4,0), @linecost numeric (7,2) OUTPUT as Begin Declare @QTY numeric(4,0) Set @linecost = 0 Select @qty = stock_level from stock where stock_code = @stkcd If @qty > @qtyreq Set @qty = @qty - @qtyreq print 'Quantity required = ' select @qtyreq Set @linecost = (select unit_price from stock where stock_code = @stkcd) print 'cost = ' select @linecost set @linecost = @linecost* @qtyreq print 'line cost = ' Update stock set stock_level = @qty where stock_code = @stkcd select @qty = quantity from order_line where stock_code = @stkcd and order_id =@dckt If @qty is null Insert into order_line values (@qtyreq, @stkcd, @dckt) else Update order_line set quantity = quantity + @qtyreq where stock_code = @stkcd and order_id = @dckt End print 'There are not enough items in stock'; end GO

To invoke sell stock and show results declare @cost numeric (7,2) set @cost = 0 exec sp_sellstock 5, e101, 2, @cost output; print 'cost =' select @cost; select * from jcustomer; select * from customerorder; select * from order_line; select * from stock; select * from Supplier;

Uses of stored Procedures This particular procedure can be used when Orders are being added. It only adds the orderline. All of the other information must be added outside this. Remember, most databases are accessed from an outside source such as JSP, ASP or Visual Basic.

Recording Joe’s Docket Populating multiple tables

Tables involved

How do you get the information in? Normally, this is done using a GUI, which populates a number of variables. We will cheat, by giving the variables values. Assume the stock being requested is: 10 of 3321 held as @QTY1, @STK1 14 of 5555 held as @QTY2, @STK2 2 of 1212 held as @QTY3, @STK3

We need to Generate a new docket number. Repeatedly ENTER the stock code and quantity being requested. Check to see if that stock is available. If so, decrement it from the stock and allocate it to the orderline. Write a new orderline. Put the system date and time onto the docket Write the docket.

Approach. 1. Have a rolling currentdocket, set on application launch. 2. Declare a function SellStock (DocketNo, StockCode, Quantity) This will take in the DocketNo, stock code and quantity and Check the stock table to see if the code exists and if there is enough stock. Write a new stock line. Return a cost value for the stock.

Generate a new docket number. This will be numerically one after the last docket. In the morning, when the system starts up, the last docket number used can be selected from the table: Declare @currentdocket int, docketdate datetime Select @currentdocket = top docketno from docket Before every new docket: Set @currentdocket = @currentdocket + 1

Repeatedly Write the docket. Repeatedly Select @Docketdate GETDATE() Insert into docket values docketno,,,docketdate Repeatedly Execute the SELLSTOCK procedure, varying the actual parameter values. Accumulate the linecosts from each sale

Not using interface – use variables variable_name := value SET @variable_name = value SELECT expression[, ...n] INTO variable_name[, ...n] FROM... SELECT {@variable_name = expression}[, ...n] FROM... FETCH cursor_name INTO variable_name[, ...n] FETCH [fetch option] FROM cursor_name INTO variable_name[, ...n]