Commit e004029e authored by michael lundquist's avatar michael lundquist
Browse files

COMMITTING A TON OF CRAP

parent 5a252196
/**
4.9-4.10 (Part II) for Chapter 4
*/
/**
4.9
Create a block to retrieve and display pledge and payment information for a specific donor.
For each pledge payment from the donor, display the pledge ID, pledge amount, number of monthly payments, payment date, and payment amount.
The list should be sorted by pledge ID and then by payment date. For the first payment made for each pledge, display "first payment" on that output row.
*/
declare
CURSOR pledges IS
SELECT IDPLEDGE, PAYAMT, PAYMONTHS, PAYDATE
FROM DD_PAYMENT JOIN DD_PLEDGE USING (IDPLEDGE)
order by IDPLEDGE, PAYDATE;
lv_id_num DD_PLEDGE.IDPLEDGE%TYPE := 0;
lv_text_str VARCHAR2(100) := '';
begin
FOR i IN pledges LOOP
if i.IDPLEDGE != lv_id_num THEN
lv_text_str := 'first payment ';
lv_id_num := i.IDPLEDGE;
else
lv_text_str := '';
end if;
dbms_output.put_line( lv_text_str || 'ID: ' || i.IDPLEDGE || ' amt: ' || i.PAYAMT || ' months: ' || i.PAYMONTHS || ' paydate: ' || i.PAYDATE );
END LOOP;
end;
/
/**
problem 10
re-do 9, with a different cursor
*/
declare
pldege_cur SYS_REFCURSOR;
lv_id_num DD_PLEDGE.IDPLEDGE%TYPE := 0;
lv_text_str VARCHAR2(100) := '';
TYPE pledge_row IS RECORD(
ID DD_PAYMENT.ID_PLEDGE%TYPE,
AMT DD_PAYMENT.PAYAMT%TYPE,
MONTHS DD_PLEDGE.PAYMONTHS%TYPE,
PAYDATE DD_PAYMENT.PAYDATE%TYPE
);
i pledge_row;
begin
open pledge_cur for SELECT IDPLEDGE, PAYAMT, PAYMONTHS, PAYDATE
FROM DD_PAYMENT JOIN DD_PLEDGE USING (IDPLEDGE)
order by IDPLEDGE, PAYDATE;
FOR i IN pledge_cur LOOP
if i.IDPLEDGE != lv_id_num THEN
lv_text_str := 'first payment ';
lv_id_num := i.IDPLEDGE;
else
lv_text_str := '';
end if;
dbms_output.put_line( lv_text_str || 'ID: ' || i.IDPLEDGE || ' amt: ' || i.PAYAMT || ' months: ' || i.PAYMONTHS || ' paydate: ' || i.PAYDATE );
END LOOP;
end;
/
\ No newline at end of file
/**
Write appropriate PL/SQL programs to complete Hands-On Assignments 4.1-.4.4 (Part I) for Chapter 4 given in the textbook. Use scripts provided above when needed.
*/
/**
4.1
Use the program to check if items are in stock
*/
DECLARE
CURSOR cur_basket(lv_custnum_numb bb_basketitem.idBasket%TYPE) IS
SELECT bi.idBasket, bi.quantity, p.stock
FROM bb_basketitem bi INNER JOIN bb_product p
USING (idProduct)
WHERE bi.idBasket = lv_custnum_numb;
TYPE type_basket IS RECORD (
basket bb_basketitem.idBasket%TYPE,
qty bb_basketitem.quantity%TYPE,
stock bb_product.stock%TYPE);
rec_basket type_basket;
lv_flag_txt CHAR(1) := 'Y';
BEGIN
OPEN cur_basket(6);
LOOP
FETCH cur_basket INTO rec_basket;
EXIT WHEN cur_basket%NOTFOUND;
IF rec_basket.stock < rec_basket.qty
THEN lv_flag_txt := 'N';
END IF;
END LOOP;
CLOSE cur_basket;
IF lv_flag_txt = 'Y'
THEN DBMS_OUTPUT.PUT_LINE('All items in stock!');
END IF;
IF lv_flag_txt = 'N'
THEN DBMS_OUTPUT.PUT_LINE('All items NOT in stock!');
END IF;
OPEN cur_basket(3);
LOOP
FETCH cur_basket INTO rec_basket;
EXIT WHEN cur_basket%NOTFOUND;
IF rec_basket.stock < rec_basket.qty
THEN lv_flag_txt := 'N';
END IF;
END LOOP;
CLOSE cur_basket;
IF lv_flag_txt = 'Y'
THEN DBMS_OUTPUT.PUT_LINE('All items in stock!');
END IF;
IF lv_flag_txt = 'N'
THEN DBMS_OUTPUT.PUT_LINE('All items NOT in stock!');
END IF;
END;
/
/**
4.2
*/
DECLARE
CURSOR cur_shopper IS
SELECT a.idShopper, a.promo, b.total
FROM bb_shopper a,
(SELECT b.idShopper, SUM(bi.quantity*bi.price) total
FROM bb_basketitem bi, bb_basket b
WHERE bi.idBasket = b.idBasket
GROUP BY idShopper) b
WHERE a.idShopper = b.idShopper
FOR UPDATE OF a.idShopper NOWAIT;
lv_promo_txt CHAR(1);
BEGIN
FOR rec_shopper IN cur_shopper LOOP
lv_promo_txt := 'X';
IF rec_shopper.total > 100 THEN
lv_promo_txt := 'A';
END IF;
IF rec_shopper.total BETWEEN 50 AND 99 THEN
lv_promo_txt := 'B';
END IF;
IF lv_promo_txt <> 'X' THEN
UPDATE bb_shopper
SET promo = lv_promo_txt
WHERE CURRENT OF cur_shopper;
END IF;
END LOOP;
COMMIT;
END;
/
/**
4.3
*/
UPDATE bb_shopper SET promo = NULL;
UPDATE bb_shopper SET promo = 'B' WHERE idShopper IN (21,23,25);
UPDATE bb_shopper SET promo = 'A' WHERE idShopper = 22;
COMMIT;
DECLARE
CURSOR promo_up IS
select idshopper, promo
from bb_shopper where not promo is null
FOR UPDATE OF promo NOWAIT;
lv_i_num NUMBER(4) := 0;
BEGIN
FOR promo_var in promo_up loop
lv_i_num := lv_i_num + 1;
update bb_shopper set promo = NULL
where current of promo_up;
end loop;
commit;
dbms_output.put_line( 'rows updated: ' || lv_i_num );
END;
/
select idshopper, promo from bb_shopper where not promo is null;
/**
4.4
*/
DECLARE
lv_tax_num NUMBER(2,2);
BEGIN
CASE 'NJ'
WHEN 'VA' THEN lv_tax_num := .04;
WHEN 'NC' THEN lv_tax_num := .02;
WHEN 'NY' THEN lv_tax_num := .06;
END CASE;
DBMS_OUTPUT.PUT_LINE('tax rate = '||lv_tax_num);
exception
when CASE_NOT_FOUND THEN
dbms_outPUT.put_line( 'NO TAX' );
END;
/
\ No newline at end of file
DECLARE
CURSOR cur_basket(lv_custnum_numb bb_basketitem.idBasket%TYPE) IS
SELECT bi.idBasket, bi.quantity, p.stock
FROM bb_basketitem bi INNER JOIN bb_product p
USING (idProduct)
WHERE bi.idBasket = lv_custnum_numb;
TYPE type_basket IS RECORD (
basket bb_basketitem.idBasket%TYPE,
qty bb_basketitem.quantity%TYPE,
stock bb_product.stock%TYPE);
rec_basket type_basket;
lv_flag_txt CHAR(1) := 'Y';
BEGIN
OPEN cur_basket(6);
LOOP
FETCH cur_basket INTO rec_basket;
EXIT WHEN cur_basket%NOTFOUND;
IF rec_basket.stock < rec_basket.qty
THEN lv_flag_txt := 'N';
END IF;
END LOOP;
CLOSE cur_basket;
IF lv_flag_txt = 'Y'
THEN DBMS_OUTPUT.PUT_LINE('All items in stock!');
END IF;
IF lv_flag_txt = 'N'
THEN DBMS_OUTPUT.PUT_LINE('All items NOT in stock!');
END IF;
OPEN cur_basket(3);
LOOP
FETCH cur_basket INTO rec_basket;
EXIT WHEN cur_basket%NOTFOUND;
IF rec_basket.stock < rec_basket.qty
THEN lv_flag_txt := 'N';
END IF;
END LOOP;
CLOSE cur_basket;
IF lv_flag_txt = 'Y'
THEN DBMS_OUTPUT.PUT_LINE('All items in stock!');
END IF;
IF lv_flag_txt = 'N'
THEN DBMS_OUTPUT.PUT_LINE('All items NOT in stock!');
END IF;
END;
/
\ No newline at end of file
DECLARE
CURSOR cur_shopper IS
SELECT a.idShopper, a.promo, b.total
FROM bb_shopper a,
(SELECT b.idShopper, SUM(bi.quantity*bi.price) total
FROM bb_basketitem bi, bb_basket b
WHERE bi.idBasket = b.idBasket
GROUP BY idShopper) b
WHERE a.idShopper = b.idShopper
FOR UPDATE OF a.idShopper NOWAIT;
lv_promo_txt CHAR(1);
BEGIN
FOR rec_shopper IN cur_shopper LOOP
lv_promo_txt := 'X';
IF rec_shopper.total > 100 THEN
lv_promo_txt := 'A';
END IF;
IF rec_shopper.total BETWEEN 50 AND 99 THEN
lv_promo_txt := 'B';
END IF;
IF lv_promo_txt <> 'X' THEN
UPDATE bb_shopper
SET promo = lv_promo_txt
WHERE CURRENT OF cur_shopper;
END IF;
END LOOP;
COMMIT;
END;
/
/**
test
*/
SELECT idShopper, s.promo, SUM(bi.quantity * bi.price) total from bb_shopper s INNER JOIN bb_basket b using (idShopper) INNER JOIN bb_basketitem bi USING (idBasket) GROUP BY idShopper, s.promo ORDER BY idShopper;
UPDATE bb_shopper SET promo = NULL;
UPDATE bb_shopper SET promo = 'B' WHERE idShopper IN (21,23,25);
UPDATE bb_shopper SET promo = 'A' WHERE idShopper = 22;
COMMIT;
DECLARE
CURSOR promo_up IS
select idshopper, promo
from bb_shopper where not promo is null
FOR UPDATE OF promo NOWAIT;
lv_i_num NUMBER(4) := 0;
BEGIN
FOR promo_var in promo_up loop
lv_i_num := lv_i_num + 1;
update bb_shopper set promo = NULL
where current of promo_up;
end loop;
commit;
dbms_output.put_line( 'rows updated: ' || lv_i_num );
END;
/
select idshopper, promo from bb_shopper where not promo is null;
\ No newline at end of file
DECLARE
lv_tax_num NUMBER(2,2);
BEGIN
CASE 'NJ'
WHEN 'VA' THEN lv_tax_num := .04;
WHEN 'NC' THEN lv_tax_num := .02;
WHEN 'NY' THEN lv_tax_num := .06;
END CASE;
DBMS_OUTPUT.PUT_LINE('tax rate = '||lv_tax_num);
exception
when CASE_NOT_FOUND THEN
dbms_outPUT.put_line( 'NO TAX' );
END;
/
\ No newline at end of file
This diff is collapsed.
# assignment 10 (ch 4) hands on
## 4.1
- Use the program to check if items are in stock
```plsql
DECLARE
CURSOR cur_basket(lv_custnum_numb bb_basketitem.idBasket%TYPE) IS
SELECT bi.idBasket, bi.quantity, p.stock
FROM bb_basketitem bi INNER JOIN bb_product p
USING (idProduct)
WHERE bi.idBasket = lv_custnum_numb;
TYPE type_basket IS RECORD (
basket bb_basketitem.idBasket%TYPE,
qty bb_basketitem.quantity%TYPE,
stock bb_product.stock%TYPE);
rec_basket type_basket;
lv_flag_txt CHAR(1) := 'Y';
BEGIN
OPEN cur_basket(6);
LOOP
FETCH cur_basket INTO rec_basket;
EXIT WHEN cur_basket%NOTFOUND;
IF rec_basket.stock < rec_basket.qty
THEN lv_flag_txt := 'N';
END IF;
END LOOP;
CLOSE cur_basket;
IF lv_flag_txt = 'Y'
THEN DBMS_OUTPUT.PUT_LINE('All items in stock!');
END IF;
IF lv_flag_txt = 'N'
THEN DBMS_OUTPUT.PUT_LINE('All items NOT in stock!');
END IF;
OPEN cur_basket(3);
LOOP
FETCH cur_basket INTO rec_basket;
EXIT WHEN cur_basket%NOTFOUND;
IF rec_basket.stock < rec_basket.qty
THEN lv_flag_txt := 'N';
END IF;
END LOOP;
CLOSE cur_basket;
IF lv_flag_txt = 'Y'
THEN DBMS_OUTPUT.PUT_LINE('All items in stock!');
END IF;
IF lv_flag_txt = 'N'
THEN DBMS_OUTPUT.PUT_LINE('All items NOT in stock!');
END IF;
END;
/
```
![4.1](./pics/P1.PNG)
## 4.2
```plsql
DECLARE
CURSOR cur_shopper IS
SELECT a.idShopper, a.promo, b.total
FROM bb_shopper a,
(SELECT b.idShopper, SUM(bi.quantity*bi.price) total
FROM bb_basketitem bi, bb_basket b
WHERE bi.idBasket = b.idBasket
GROUP BY idShopper) b
WHERE a.idShopper = b.idShopper
FOR UPDATE OF a.idShopper NOWAIT;
lv_promo_txt CHAR(1);
BEGIN
FOR rec_shopper IN cur_shopper LOOP
lv_promo_txt := 'X';
IF rec_shopper.total > 100 THEN
lv_promo_txt := 'A';
END IF;
IF rec_shopper.total BETWEEN 50 AND 99 THEN
lv_promo_txt := 'B';
END IF;
IF lv_promo_txt <> 'X' THEN
UPDATE bb_shopper
SET promo = lv_promo_txt
WHERE CURRENT OF cur_shopper;
END IF;
END LOOP;
COMMIT;
END;
/
```
![p2a](./pics/P2A.PNG)
![p2b](./pics/P2B.PNG)
## 4.3
```plsql
UPDATE bb_shopper SET promo = NULL;
UPDATE bb_shopper SET promo = 'B' WHERE idShopper IN (21,23,25);
UPDATE bb_shopper SET promo = 'A' WHERE idShopper = 22;
COMMIT;
DECLARE
CURSOR promo_up IS
select idshopper, promo
from bb_shopper where not promo is null
FOR UPDATE OF promo NOWAIT;
lv_i_num NUMBER(4) := 0;
BEGIN
FOR promo_var in promo_up loop
lv_i_num := lv_i_num + 1;
update bb_shopper set promo = NULL
where current of promo_up;
end loop;
commit;
dbms_output.put_line( 'rows updated: ' || lv_i_num );
END;
/
select idshopper, promo from bb_shopper where not promo is null;
```
![p3a](./pics/P3A.PNG)
![p3b](./pics/P3B.PNG)
![p3c](./pics/p3c.PNG)
## 4.4
```plsql
DECLARE
lv_tax_num NUMBER(2,2);
BEGIN
CASE 'NJ'
WHEN 'VA' THEN lv_tax_num := .04;
WHEN 'NC' THEN lv_tax_num := .02;
WHEN 'NY' THEN lv_tax_num := .06;
END CASE;
DBMS_OUTPUT.PUT_LINE('tax rate = '||lv_tax_num);
exception
when CASE_NOT_FOUND THEN
dbms_outPUT.put_line( 'NO TAX' );
END;
/
```
![p4](./pics/P4.PNG)
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment