Moved Queries-A5 to Queries-A6 since it's student marks procedure which is Assignment A6. Thanks to Shivani for pointing it out.
This commit is contained in:
parent
7649d531ba
commit
82db5216cb
@ -1,76 +0,0 @@
|
||||
## Creating tables & inserting data
|
||||
|
||||
Student marks:
|
||||
```sql
|
||||
CREATE TABLE Stud_Marks(
|
||||
Roll NUMBER(14),
|
||||
name VARCHAR(255),
|
||||
total_marks NUMBER(14),
|
||||
PRIMARY KEY(Roll)
|
||||
);
|
||||
|
||||
INSERT INTO Stud_Marks VALUES (1, 'Arora', 1350);
|
||||
INSERT INTO Stud_Marks VALUES (2, 'Shankar', 979);
|
||||
INSERT INTO Stud_Marks VALUES (3, 'Lara', 834);
|
||||
INSERT INTO Stud_Marks VALUES (4, 'Jambo', 667);
|
||||
INSERT INTO Stud_Marks VALUES (5, 'Tinde', 1400);
|
||||
|
||||
```
|
||||
|
||||
Result:
|
||||
```sql
|
||||
CREATE TABLE Result(
|
||||
Roll NUMBER(14),
|
||||
name VARCHAR(255),
|
||||
Class VARCHAR(255),
|
||||
FOREIGN KEY (Roll) REFERENCES Stud_Marks (Roll)
|
||||
);
|
||||
|
||||
```
|
||||
|
||||
## Procedure
|
||||
```sql
|
||||
CREATE OR REPLACE PROCEDURE proc_grade
|
||||
(rollno in number,
|
||||
p_roll_no out Stud_Marks.Roll%type,
|
||||
p_name out Stud_Marks.name%type,
|
||||
p_total out Stud_Marks.total_marks%type)
|
||||
AS
|
||||
BEGIN
|
||||
SELECT Roll, name, total_marks into p_roll_no, p_name, p_total from Stud_Marks WHERE Roll = rollno;
|
||||
IF p_total <= 1500 and p_total >= 990
|
||||
THEN
|
||||
INSERT INTO Result VALUES (p_roll_no, p_name, 'Distinction');
|
||||
ELSE IF p_total <= 989 and p_total >= 900
|
||||
THEN
|
||||
INSERT INTO Result VALUES (p_roll_no, p_name, 'First Class');
|
||||
ELSE IF p_total <= 899 and p_total >= 825
|
||||
THEN
|
||||
INSERT INTO Result VALUES (p_roll_no, p_name, 'Higher Second Class');
|
||||
ELSE
|
||||
INSERT INTO Result VALUES (p_roll_no, p_name, 'FAIL');
|
||||
END IF;
|
||||
END IF;
|
||||
END IF;
|
||||
EXCEPTION
|
||||
WHEN no_data_found THEN
|
||||
dbms_output.put_line('Rollno ' || rollno || ' is not present!!');
|
||||
END;
|
||||
/
|
||||
|
||||
```
|
||||
|
||||
## Calling Procedure:
|
||||
```sql
|
||||
DECLARE
|
||||
rollno number(14);
|
||||
p_roll_no Stud_Marks.Roll%type;
|
||||
p_name Stud_Marks.name%type;
|
||||
p_total_marks Stud_Marks.total_marks%type;
|
||||
BEGIN
|
||||
rollno := &rollno;
|
||||
proc_grade(rollno, p_roll_no, p_name, p_total_marks);
|
||||
END;
|
||||
/
|
||||
|
||||
```
|
149
Practical/Assignment-A6/Queries-A6.md
Normal file → Executable file
149
Practical/Assignment-A6/Queries-A6.md
Normal file → Executable file
@ -1,107 +1,78 @@
|
||||
## Creating tables
|
||||
# Database Queries for Assignment-A6
|
||||
|
||||
`O_RollCall` table:
|
||||
## Creating tables & inserting data
|
||||
|
||||
Student marks:
|
||||
```sql
|
||||
CREATE TABLE O_RollCall(
|
||||
name VARCHAR(255),
|
||||
roll NUMBER(14),
|
||||
class VARCHAR(255)
|
||||
CREATE TABLE Stud_Marks(
|
||||
Roll NUMBER(14),
|
||||
name VARCHAR(255),
|
||||
total_marks NUMBER(14),
|
||||
PRIMARY KEY(Roll)
|
||||
|
||||
);
|
||||
|
||||
INSERT INTO Stud_Marks VALUES (1, 'Arora', 1350);
|
||||
INSERT INTO Stud_Marks VALUES (2, 'Shankar', 979);
|
||||
INSERT INTO Stud_Marks VALUES (3, 'Lara', 834);
|
||||
INSERT INTO Stud_Marks VALUES (4, 'Jambo', 667);
|
||||
INSERT INTO Stud_Marks VALUES (5, 'Tinde', 1400);
|
||||
|
||||
```
|
||||
|
||||
`N_RollCall` table:
|
||||
Result:
|
||||
```sql
|
||||
CREATE TABLE N_RollCall(
|
||||
name VARCHAR(255),
|
||||
roll NUMBER(14),
|
||||
class VARCHAR(255)
|
||||
CREATE TABLE Result(
|
||||
Roll NUMBER(14),
|
||||
name VARCHAR(255),
|
||||
Class VARCHAR(255),
|
||||
FOREIGN KEY (Roll) REFERENCES Stud_Marks (Roll)
|
||||
);
|
||||
|
||||
```
|
||||
|
||||
## Inserting data
|
||||
|
||||
`O_RollCall` table:
|
||||
```sql
|
||||
INSERT INTO O_RollCall VALUES ('Stewie', 1, 'Comp 1');
|
||||
INSERT INTO O_RollCall VALUES ('Edie', 2, 'Comp 2');
|
||||
INSERT INTO O_RollCall VALUES ('Stomp', 3, 'Comp 3');
|
||||
INSERT INTO O_RollCall VALUES ('Lara', 4, 'Comp 1');
|
||||
INSERT INTO O_RollCall VALUES ('Foxy', 5, 'Comp 2');
|
||||
|
||||
```
|
||||
|
||||
`N_RollCall` table:
|
||||
```sql
|
||||
INSERT INTO N_RollCall VALUES ('Stewie', 1, 'Comp 1');
|
||||
INSERT INTO N_RollCall VALUES ('Edie', 2, 'Comp 2');
|
||||
INSERT INTO N_RollCall VALUES ('Stomp', 3, 'Comp 3');
|
||||
INSERT INTO N_RollCall VALUES ('Lara', 4, 'Comp 1');
|
||||
INSERT INTO N_RollCall VALUES ('Foxy', 5, 'Comp 2');
|
||||
INSERT INTO N_RollCall VALUES ('Gundeti', 6, 'Comp 3');
|
||||
INSERT INTO N_RollCall VALUES ('Kalas', 7, 'Comp 2');
|
||||
|
||||
```
|
||||
|
||||
## Procedure
|
||||
|
||||
### Explicit cursor
|
||||
```sql
|
||||
DECLARE
|
||||
p_name VARCHAR(255);
|
||||
p_rollno NUMBER(15);
|
||||
p_class VARCHAR(255);
|
||||
CURSOR cc1 IS SELECT * FROM N_RollCall WHERE roll NOT IN (SELECT roll FROM O_RollCall);
|
||||
CREATE OR REPLACE PROCEDURE proc_grade
|
||||
(rollno in number,
|
||||
p_roll_no out Stud_Marks.Roll%type,
|
||||
p_name out Stud_Marks.name%type,
|
||||
p_total out Stud_Marks.total_marks%type)
|
||||
AS
|
||||
BEGIN
|
||||
OPEN cc1;
|
||||
LOOP
|
||||
FETCH cc1 INTO p_name, p_rollno, p_class;
|
||||
INSERT INTO O_RollCall VALUES (p_name, p_rollno, p_class);
|
||||
EXIT WHEN cc1%notfound;
|
||||
DBMS_OUTPUT.PUT_LINE(p_name || ' ' || p_rollno || ' ' || p_class);
|
||||
END LOOP;
|
||||
CLOSE cc1;
|
||||
END;
|
||||
/
|
||||
|
||||
```
|
||||
|
||||
### Parameterized cursor
|
||||
```sql
|
||||
DECLARE
|
||||
p_name VARCHAR(255);
|
||||
p_rollno NUMBER(15);
|
||||
p_class VARCHAR(255);
|
||||
CURSOR pp1(roll1 NUMBER) IS SELECT * FROM N_RollCall WHERE roll > roll1;
|
||||
|
||||
BEGIN
|
||||
OPEN pp1(3);
|
||||
LOOP
|
||||
FETCH pp1 INTO p_name, p_rollno, p_class;
|
||||
EXIT WHEN pp1%notfound;
|
||||
DBMS_OUTPUT.PUT_LINE(p_name || ' ' || p_rollno || ' ' || p_class);
|
||||
END LOOP;
|
||||
CLOSE pp1;
|
||||
|
||||
END;
|
||||
/
|
||||
|
||||
```
|
||||
|
||||
### Implicit Cursor
|
||||
```sql
|
||||
DECLARE
|
||||
total_rows NUMBER(2);
|
||||
|
||||
BEGIN
|
||||
UPDATE N_RollCall SET roll = roll + 1;
|
||||
IF sql%notfound THEN
|
||||
dbms_output.put_line('no roll was updated');
|
||||
ELSIF sql%found THEN
|
||||
total_rows := sql%rowcount;
|
||||
DBMS_OUTPUT.PUT_LINE( total_rows || ' roll calls were affected ');
|
||||
SELECT Roll, name, total_marks into p_roll_no, p_name, p_total from Stud_Marks WHERE Roll = rollno;
|
||||
IF p_total <= 1500 and p_total >= 990
|
||||
THEN
|
||||
INSERT INTO Result VALUES (p_roll_no, p_name, 'Distinction');
|
||||
ELSE IF p_total <= 989 and p_total >= 900
|
||||
THEN
|
||||
INSERT INTO Result VALUES (p_roll_no, p_name, 'First Class');
|
||||
ELSE IF p_total <= 899 and p_total >= 825
|
||||
THEN
|
||||
INSERT INTO Result VALUES (p_roll_no, p_name, 'Higher Second Class');
|
||||
ELSE
|
||||
INSERT INTO Result VALUES (p_roll_no, p_name, 'FAIL');
|
||||
END IF;
|
||||
END IF;
|
||||
END IF;
|
||||
EXCEPTION
|
||||
WHEN no_data_found THEN
|
||||
dbms_output.put_line('Rollno ' || rollno || ' is not present!!');
|
||||
END;
|
||||
/
|
||||
|
||||
```
|
||||
|
||||
## Calling Procedure:
|
||||
```sql
|
||||
DECLARE
|
||||
rollno number(14);
|
||||
p_roll_no Stud_Marks.Roll%type;
|
||||
p_name Stud_Marks.name%type;
|
||||
p_total_marks Stud_Marks.total_marks%type;
|
||||
BEGIN
|
||||
rollno := &rollno;
|
||||
proc_grade(rollno, p_roll_no, p_name, p_total_marks);
|
||||
END;
|
||||
/
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user