Presentation is loading. Please wait.

Presentation is loading. Please wait.

Task oriented processing

Similar presentations


Presentation on theme: "Task oriented processing"— Presentation transcript:

1 Task oriented processing
And Queries

2 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

3 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?

4 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?

5 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?

6 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.

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

8 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>.

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

10 Using Variables Variables can be assigned values using Set:
= 0; * 1.1; Or using Select: numeric(7,0); = = suppliername from Supplier where SupplierId

11 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.

12 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.

13 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

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

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

16 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.

17 Recording Joe’s Docket
Populating multiple tables

18 Tables involved

19 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 14 of 5555 2 of 1212

20 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.

21 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.

22 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: int, docketdate datetime = top docketno from docket Before every new docket: + 1

23 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

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


Download ppt "Task oriented processing"

Similar presentations


Ads by Google