Q: Write a PL/SQL after creating following tables with appropriate dataypes and constraints.
Purchase_Header (Purchase_no, Purchase_date, Vendor_id, Remarks)
Purchase_detail (Purchase_no, sr_no, item_id, qty)
Vendor_master (Vendor_id, Vendor_name)
Item_master(Item_id, Item_name, unit_rate)
Insert two records in Purchase_header, at least five records in purchase_detail and three records each in remaining tables.
The PL/SQL block to generate following output:
Purchase_no Purcahse_date Vendor_name sr_no item_name unit_rate Quantity Amount
Answers
Answer:
CREATE TABLE employees_temp AS SELECT employee_id, first_name, last_name
FROM employees;
DECLARE
emp_id employees_temp.employee_id%TYPE;
emp_first_name employees_temp.first_name%TYPE;
emp_last_name employees_temp.last_name%TYPE;
BEGIN
INSERT INTO employees_temp VALUES(299, 'Bob', 'Henry');
UPDATE employees_temp SET first_name = 'Robert' WHERE employee_id = 299;
DELETE FROM employees_temp WHERE employee_id = 299
RETURNING first_name, last_name INTO emp_first_name, emp_last_name;
COMMIT;
DBMS_OUTPUT.PUT_LINE( emp_first_name || ' ' || emp_last_name);
END;
/
- CREATE TABLE employees_temp AS SELECT * FROM employees;
- BEGIN
- UPDATE employees_temp SET salary = salary * 1.05 WHERE salary < 5000;
- DBMS_OUTPUT.PUT_LINE('Updated ' || SQL%ROWCOUNT || ' salaries.');
- END;
- /
- CREATE TABLE employees_temp AS SELECT first_name, last_name FROM employees;
- DECLARE
- x VARCHAR2(20) := 'my_first_name';
- y VARCHAR2(25) := 'my_last_name';
- BEGIN
- INSERT INTO employees_temp VALUES(x, y);
- UPDATE employees_temp SET last_name = x WHERE first_name = y;
- DELETE FROM employees_temp WHERE first_name = x;
- COMMIT;
- END;
- /
- DECLARE
- job_count NUMBER;
- emp_count NUMBER;
- BEGIN
- SELECT COUNT(DISTINCT job_id) INTO job_count FROM employees;
- SELECT COUNT(*) INTO emp_count FROM employees;
- END;
- /
4.
CREATE TABLE employees_temp AS SELECT employee_id, first_name, last_name
FROM employees;
CREATE TABLE employees_temp2 AS SELECT employee_id, first_name, last_name
FROM employees;
DECLARE
seq_value NUMBER;
BEGIN
-- Display initial value of NEXTVAL
-- This is invalid: seq_value := employees_seq.NEXTVAL;
SELECT employees_seq.NEXTVAL INTO seq_value FROM dual;
DBMS_OUTPUT.PUT_LINE ('Initial sequence value: ' || TO_CHAR(seq_value));
-- The NEXTVAL value is the same no matter what table you select from
-- You usually use NEXTVAL to create unique numbers when inserting data.
INSERT INTO employees_temp VALUES (employees_seq.NEXTVAL, 'Lynette', 'Smith');
-- If you need to store the same value somewhere else, you use CURRVAL
INSERT INTO employees_temp2 VALUES (employees_seq.CURRVAL, 'Morgan', 'Smith');
-- Because NEXTVAL values might be referenced by different users and
-- applications, and some NEXTVAL values might not be stored in the
-- database, there might be gaps in the sequence
-- The following uses the stored value of the CURRVAL in seq_value to specify
-- the record to delete because CURRVAL (or NEXTVAL) cannot used in a WHERE clause
-- This is invalid: WHERE employee_id = employees_seq.CURRVAL;
SELECT employees_seq.CURRVAL INTO seq_value FROM dual;
DELETE FROM employees_temp2 WHERE employee_id = seq_value;
-- The following udpates the employee_id with NEXTVAL for the specified record
UPDATE employees_temp SET employee_id = employees_seq.NEXTVAL
WHERE first_name = 'Lynette' AND last_name = 'Smith';
-- Display end value of CURRVAL
SELECT employees_seq.CURRVAL INTO seq_value FROM dual;
DBMS_OUTPUT.PUT_LINE ('Ending sequence value: ' || TO_CHAR(seq_value));
END;
/