-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBMS Assignment 8
More file actions
167 lines (114 loc) · 3.27 KB
/
DBMS Assignment 8
File metadata and controls
167 lines (114 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
Problem Statement:
1.Write an anonymous code block which will update marks of students to 40 if he has scored between 35 to 39.Using implicit cursor parameters show database whether any records have been updated or not.If updated,display how many records have been updated.
SQL> create table student(roll_no int,name varchar(20),marks int);
Table created.
SQL> insert into student values(1,'abc',39);
1 row created.
SQL> insert into student values(2,'pqr',35);
1 row created.
SQL> insert into student values(3,'xyz',41);
1 row created.
SQL> insert into student values(4,'cde',37);
1 row created.
SQL> insert into student values(5,'lmo',46);
1 row created.
SQL> select * from student;
ROLL_NO NAME MARKS
---------- -------------------- ----------
1 abc 39
2 pqr 35
3 xyz 41
4 cde 37
5 lmo 46
begin
update student set marks=40 where marks between 35 and 39;
if SQL%notfound then
dbms_output.put_line('No records were updated');
else
dbms_output.put_line('Total records updated:'||sql%rowcount);
end if;
end;
9 /
PL/SQL procedure successfully completed.
SQL> select * from student;
ROLL_NO NAME MARKS
---------- -------------------- ----------
1 abc 40
2 pqr 40
3 xyz 41
4 cde 40
5 lmo 46
SQL> set serveroutput on;
declare
begin
update student set marks=40 where marks between 35 and 39;
if SQL%notfound then
dbms_output.put_line('No records were updated');
else
dbms_output.put_line('Total records updated:'||sql%rowcount);
end if;
end;
10 /
No records were updated
PL/SQL procedure successfully completed.
declare
begin
update student set marks=45 where marks between 35 and 44;
if SQL%notfound then
dbms_output.put_line('No records were updated');
else
dbms_output.put_line('Total records updated:'||sql%rowcount);
end if;
end;
10 /
Total records updated:4
PL/SQL procedure successfully completed.
SQL> select * from student;
ROLL_NO NAME MARKS
---------- -------------------- ----------
1 abc 45
2 pqr 45
3 xyz 45
4 cde 45
5 lmo 46
2.Write an anonymous code block to demonstrate use of explicit cursor,for loop & parametrized explicit cursor.Copy the content of student table to another table.Before copying,check whether second table consists of same roll number record.If so,discard it otherwise copy it.
SQL> create table newstudent(roll_no int,name varchar(20),marks int);
Table created.
SQL> insert into newstudent values(1,'abc',45);
1 row created.
SQL> insert into newstudent values(3,'xyz',45);
1 row created.
SQL> insert into newstudent values(7,'xyzr',95);
1 row created.
SQL> insert into newstudent values(8,'pqrs',65);
1 row created.
declare
cursor cur_s is select * from student;
cursor cur_new(a int) is select * from newstudent where roll_no =a;
nrec newstudent%rowtype;
5
begin
for srec in cur_s
loop
open cur_new(srec.roll_no);
fetch cur_new into nrec;
if cur_new%notfound then
insert into newstudent values(srec.roll_no,srec.name,srec.marks);
end if;
close cur_new;
end loop;
end;
17 /
PL/SQL procedure successfully completed.
SQL> SELECT * FROM newstudent;
ROLL_NO NAME MARKS
---------- -------------------- ----------
1 abc 45
3 xyz 45
7 xyzr 95
8 pqrs 65
2 pqr 45
4 cde 45
5 lmo 46
7 rows selected.
SQL>