-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplsql practice.txt
1633 lines (1282 loc) · 42.2 KB
/
plsql practice.txt
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
### Variables, Constant, Anchor Data types ###
set serveroutput on
DECLARE
name varchar2(15);
BEGIN
name := 'gowthamraj';
DBMS_OUTPUT.PUT_LINE('my name is : '||name);
END;
/
********************************************************
DECLARE
val constant number := 10;
BEGIN
DBMS_OUTPUT.PUT_LINE('value : '||val);
END;
/
*******************************************************
DECLARE
v_pi CONSTANT number(7,6) NOT NULL DEFAULT 3.141592;
BEGIN
DBMS_OUTPUT.PUT_LINE(v_pi);
END;
/
********************************************************
set autoprint on;
variable v_bind2 varchar2(10);
exec :v_bind2 := 'Gowtham';
********************************************************
VARIABLE v_bind1 varchar2(10);
EXEC :v_bind1 := 'gowthamraj';
BEGIN
:v_bind1 := 'Gowtham';
DBMS_OUTPUT.PUT_LINE(:v_bind1);
END;
/
********************************************************
### Custom Prompt box to get user input
ACCEPT age number PROMPT 'what is your age ?';
*******************************************************
### Conditional Statements ###
DECLARE
num number := 8;
BEGIN
IF num<10 THEN
dbms_output.put_line('Inside If');
END IF;
dbms_output.put_line('outside If');
END;
/
********************************************************
DECLARE
num number := -8;
BEGIN
IF num < 10 AND num > 0 THEN
dbms_output.put_line('Inside If');
END IF;
dbms_output.put_line('outside If');
END;
/
********************************************************
DECLARE
num number := &enter_value;
BEGIN
IF MOD(num,2) = 0 THEN
dbms_output.put_line('Even Number');
ELSE
dbms_output.put_line('Odd Number');
END IF;
END;
/
********************************************************
DECLARE
num number := &enter_value;
BEGIN
IF num > 0 THEN
IF MOD(num,2) = 0 THEN
dbms_output.put_line('Even Number');
ELSE
dbms_output.put_line('Odd Number');
END IF;
ELSIF num = 0 THEN
dbms_output.put_line('Zero');
ELSE
dbms_output.put_line('Negative Number');
END IF;
END;
/
********************************************************
DECLARE
num number := &enter_value;
BEGIN
IF num > 0 THEN
dbms_output.put_line('Positive Number');
ELSIF num < 0 THEN
dbms_output.put_line('Negative Number');
ELSE
dbms_output.put_line('Zero Number');
END IF;
END;
/
********************************************************
DECLARE
grade char(1) := &enter_grade;
rank varchar2(30);
BEGIN
CASE grade
WHEN 'A' THEN
rank := 'Excelent';
WHEN 'B' THEN
rank := 'Very Good';
WHEN 'C' THEN
rank := 'Good';
WHEN 'D' THEN
rank := 'Average';
WHEN 'E' THEN
rank := 'Poor';
ELSE
rank := 'No Such Grade';
END CASE;
dbms_output.put_line('Grade is : '||rank);
END;
/
********************************************************
=> Searched Case :
DECLARE
units number := &enter_total_units;
total_charge number := 1000;
BEGIN
CASE
WHEN units > 0 AND units <= 5 THEN
total_charge := total_charge + (total_charge*0.05);
WHEN units > 5 AND units <= 10 THEN
total_charge := total_charge + (total_charge*0.10);
WHEN units > 10 AND units <= 30 THEN
total_charge := total_charge + (total_charge*0.25);
WHEN units > 30 AND units <= 50 THEN
total_charge := total_charge + (total_charge*0.50);
WHEN units > 50 THEN
total_charge := total_charge*2;
ELSE
total_charge := total_charge ;
END CASE;
dbms_output.put_line('You have to pay Rs: '||total_charge );
END;
/
********************************************************
=> GOTO statement
BEGIN
GOTO message2;
<<message1>>
dbms_output.put_line('message 1 is displayed here...');
<<message2>>
dbms_output.put_line('message 2 is displayed here...');
END;
/
********************************************************
=> NULL statement
DECLARE
status boolean := true;
BEGIN
IF status THEN
GOTO message;
END IF;
<<message>>
dbms_output.put_line('message 1 is displayed here...');
NULL;
END;
/
********************************************************
### Looping Statements ###
DECLARE
counter number := 0;
BEGIN
LOOP
counter := counter + 1;
dbms_output.put_line(counter);
IF counter >= 5 THEN
EXIT;
END IF;
END LOOP;
END;
/
********************************************************
DECLARE
counter number := 0;
BEGIN
LOOP
counter := counter + 1;
dbms_output.put_line(counter);
EXIT WHEN counter >= 5;
END LOOP;
END;
/
********************************************************
DECLARE
row_counter number := 0;
col_counter number := 0;
BEGIN
<<outer_loop>> LOOP
row_counter := row_counter + 1;
EXIT outer_loop WHEN row_counter > 2;
dbms_output.put_line('Outer Loop'||row_counter);
col_counter := 0;
<<inner_loop>> LOOP
col_counter := col_counter + 1;
EXIT inner_loop WHEN col_counter > 3;
dbms_output.put_line('Inner Loop'||col_counter);
END LOOP inner_loop;
END LOOP outer_loop;
END;
/
********************************************************
DECLARE
num number := &enter_value;
counter number := 1;
result number;
BEGIN
WHILE counter <= 10 LOOP
result := num*counter;
dbms_output.put_line(counter||' * '||num||' = '||result);
counter := counter + 1;
END LOOP;
END;
/
********************************************************
DECLARE
num number := &enter_value;
counter number := 1;
flag boolean := true;
result number;
BEGIN
WHILE flag LOOP
IF counter = 10 THEN
flag := false;
END IF;
result := num*counter;
dbms_output.put_line(counter||' * '||num||' = '||result);
counter := counter + 1;
END LOOP;
END;
/
********************************************************
BEGIN
FOR counter IN 1..10 LOOP
dbms_output.put_line(counter);
END LOOP;
END;
/
********************************************************
BEGIN
FOR counter IN REVERSE 1..10 LOOP
dbms_output.put_line(counter);
END LOOP;
END;
/
********************************************************
DECLARE
num number := &enter_value;
result number;
BEGIN
FOR counter IN 1..10 LOOP
result := num*counter;
dbms_output.put_line(counter||' * '||num||' = '||result);
END LOOP;
END;
/
********************************************************
=> continue
BEGIN
FOR counter IN 1..10 LOOP
IF MOD(counter,2) = 1 THEN
CONTINUE;
END IF;
dbms_output.put_line(counter);
END LOOP;
END;
/
********************************************************
BEGIN
FOR counter IN 1..10 LOOP
CONTINUE WHEN MOD(counter,2) = 1;
dbms_output.put_line(counter);
END LOOP;
END;
/
********************************************************
### Select INTO ###
DECLARE
sal employees.salary%TYPE;
BEGIN
select salary into sal from employees where employee_id=101;
DBMS_OUTPUT.PUT_LINE('Salary is : '||sal);
END;
/
********************************************************
DECLARE
name varchar2(15);
sal number(5);
BEGIN
select concat(first_name,last_name), salary into name, sal from employees where employee_id=100;
DBMS_OUTPUT.PUT_LINE('The Employee with full name '||name||' has salary '||sal);
END;
/
********************************************************
### Record Data Types ### -> They are a datastructure which can hold data of different types.
=> Table Based Record Type
DECLARE
emp_id number := &emter_emp_id;
employee1 employees%ROWTYPE;
BEGIN
select * into employee1 from employees where employee_id = emp_id;
dbms_output.put_line('Name of employee with id '||emp_id||' : '||employee1.first_name);
END;
/
********************************************************
=> Cursor Based Record Type
DECLARE
CURSOR cursor1 IS
select * from employees where employee_id=100;
record1 cursor1%ROWTYPE;
BEGIN
OPEN cursor1;
FETCH cursor1 INTO record1;
dbms_output.put_line('Name of employee with id '||record1.employee_id||' : '||record1.first_name);
CLOSE cursor1;
END;
/
********************************************************
DECLARE
CURSOR cursor1 IS
select * from employees where employee_id in (100,101,102,103);
record1 cursor1%ROWTYPE;
BEGIN
OPEN cursor1;
LOOP
FETCH cursor1 INTO record1;
EXIT WHEN cursor1%NOTFOUND;
dbms_output.put_line('Name of employee with id '||record1.employee_id||' : '||record1.first_name);
END LOOP;
CLOSE cursor1;
END;
/
********************************************************
=> User defined Record Type
DECLARE
TYPE temp_record IS RECORD (
emp_id employees.employee_id%TYPE,
fname varchar2(30),
lname varchar2(30)
);
record1 temp_record;
BEGIN
select employee_id, first_name, last_name into record1 from employees where employee_id = 100;
dbms_output.put_line('Employee details are : '||record1.emp_id||' '||record1.fname||' '||record1.lname);
END;
/
********************************************************
=> Inserting in a Record Type
DECLARE
employee1 employees%ROWTYPE;
BEGIN
employee1.employee_id := 1001;
employee1.first_name := 'Gowtham';
employee1.last_name := 'Raj';
INSERT INTO employees values employee1;
dbms_output.put_line('Inserted Employee details are : '||employee1.employee_id||' '||employee1.first_name||' '||employee1.last_name);
END;
/
********************************************************
=> Updating in a Record Type (use SET ROW keyword)
DECLARE
employee1 employees%ROWTYPE;
BEGIN
select * into employee1 from employees where employee_id = 1001;
employee1.last_name := 'RajK';
UPDATE employees SET ROW = employee1 where employee1.employee_id = employees.employee_id;
dbms_output.put_line('Updated Employee details are : '||employee1.employee_id||' '||employee1.first_name||' '||employee1.last_name);
END;
/
********************************************************
=> Nested Records
DECLARE
TYPE address IS RECORD(
door_no number,
street varchar2(20),
state varchar2(20),
country varchar2(20)
);
TYPE customer IS RECORD(
name varchar2(30),
shipping_address address
);
customer1 customer;
BEGIN
customer1.name := 'gowthamraj';
customer1.shipping_address.door_no := 2;
customer1.shipping_address.street := 'abc';
customer1.shipping_address.state := 'TN';
customer1.shipping_address.country := 'INDIA';
dbms_output.put_line('Customer details : '||customer1.name||' '||customer1.shipping_address.door_no||' '||customer1.shipping_address.street||' '||customer1.shipping_address.state||' '||customer1.shipping_address.country);
END;
/
********************************************************
### Cursors ### -> used to collect multiple records, its like a pointer pointing to the context area of that particular DML query,
Types : -> implicit cursor, explicit cursor.
DECLARE
name varchar2(30);
CURSOR cursor1 IS
select first_name from employees where employee_id = 100;
BEGIN
OPEN cursor1;
FETCH cursor1 into name;
dbms_output.put_line('Employee Name : '||name);
CLOSE cursor1;
END;
/
********************************************************
DECLARE
name varchar2(30);
CURSOR cursor1 IS
select first_name from employees where employee_id IN (100,101,102,103);
BEGIN
OPEN cursor1;
LOOP
FETCH cursor1 into name;
dbms_output.put_line('Employee Name : '||name);
EXIT WHEN cursor1%NOTFOUND;
END LOOP;
CLOSE cursor1;
END;
/
********************************************************
=> cursor with multiple values
DECLARE
fname varchar2(30);
lname varchar2(30);
CURSOR cursor1 IS
select first_name, last_name from employees where employee_id = 101;
BEGIN
OPEN cursor1;
FETCH cursor1 into fname,lname;
dbms_output.put_line('Employee Name : '||fname||' '||lname);
CLOSE cursor1;
END;
/
********************************************************
=> Cursor with Parameter
DECLARE
name varchar2(30);
CURSOR cursor1(emp_id number) IS
select first_name from employees where employee_id = emp_id;
BEGIN
OPEN cursor1(101);
FETCH cursor1 into name;
dbms_output.put_line('Employee Name : '||name);
CLOSE cursor1;
END;
/
********************************************************
DECLARE
name varchar2(30);
CURSOR cursor1(emp_id number) IS
select first_name from employees where employee_id = emp_id;
BEGIN
OPEN cursor1(101);
FETCH cursor1 into name;
dbms_output.put_line('Employee Name : '||name);
CLOSE cursor1;
OPEN cursor1(102);
FETCH cursor1 into name;
dbms_output.put_line('Employee Name : '||name);
CLOSE cursor1;
END;
/
********************************************************
=> cursor with default parameter value
DECLARE
name varchar2(30);
CURSOR cursor1(emp_id number := 101) IS
select first_name from employees where employee_id = emp_id;
BEGIN
OPEN cursor1;
FETCH cursor1 into name;
dbms_output.put_line('Employee Name : '||name);
CLOSE cursor1;
END;
/
********************************************************
=> Cursor For Loop
DECLARE
CURSOR cursor1 IS
select * from employees where employee_id in (100,101,102,103);
BEGIN
FOR employee IN cursor1 LOOP
dbms_output.put_line('Employee Details => '||employee.employee_id||' '||employee.first_name||' '||employee.last_name||' '||employee.salary);
END LOOP;
END;
/
********************************************************
=> Cursor Forloop with select statement (Anonymous Cursors)
BEGIN
FOR employee IN (
select * from employees where employee_id in (100,101,102,103)
)
LOOP
dbms_output.put_line('Employee Details => '||employee.employee_id||' '||employee.first_name||' '||employee.last_name||' '||employee.salary);
END LOOP;
END;
/
********************************************************
=> Cursor with REF cursors (Strong typed)
DECLARE
TYPE cursor1 IS REF CURSOR RETURN employees%ROWTYPE;
ref_cursor cursor1;
employee1 employees%Rowtype;
BEGIN
OPEN ref_cursor for select * from employees where employee_id in (100,101,102,103);
LOOP
FETCH ref_cursor into employee1;
EXIT WHEN ref_cursor%NOTFOUND;
dbms_output.put_line('Employee Name : '||employee1.first_name);
END LOOP;
CLOSE ref_cursor;
END;
/
********************************************************
=> Cursor with REF cursors (Weak typed)
DECLARE
TYPE cursor1 IS REF CURSOR;
ref_cursor cursor1;
employee1 employees%Rowtype;
BEGIN
OPEN ref_cursor for select * from employees where employee_id in (100,101,102,103);
LOOP
FETCH ref_cursor into employee1;
EXIT WHEN ref_cursor%NOTFOUND;
dbms_output.put_line('Employee Name : '||employee1.first_name);
END LOOP;
CLOSE ref_cursor;
END;
/
********************************************************
=> updatable Cursor
DECLARE
CURSOR cursor1 IS
select employee_id, first_name, last_name from employees where employee_id in (100,101,102)
FOR UPDATE;
employee_id number;
first_name employees.first_name%TYPE;
last_name employees.last_name%TYPE;
BEGIN
OPEN cursor1;
LOOP
FETCH cursor1 into employee_id, first_name, last_name;
EXIT WHEN cursor1%NOTFOUND;
dbms_output.put_line('Employee Details : '||' '||employee_id||' '||first_name||' '||last_name);
END LOOP;
CLOSE cursor1;
END;
/
********************************************************
### Procedures ### -> named subprograms which can be stored and executed later, contains IN, OUT, IN OUT parameters.
=> Procedure with input parameter
CREATE OR REPLACE PROCEDURE filterByEmployeeId(emp_id IN number) IS
employee1 employees%ROWTYPE;
BEGIN
select * into employee1 from employees where employee_id = emp_id;
dbms_output.put_line('Employee '||employee1.employee_id||' '||employee1.first_name||' '||employee1.last_name||' '||employee1.salary);
END;
/
EXEC filterByEmployeeId(100);
********************************************************
CREATE OR REPLACE PROCEDURE filterByEmployeeId(emp_id IN number) IS
employee1 employees%ROWTYPE;
BEGIN
select * into employee1 from employees where employee_id = emp_id;
dbms_output.put_line('Employee '||employee1.employee_id||' '||employee1.first_name||' '||employee1.last_name||' '||employee1.salary);
END;
/
EXEC filterByEmployeeId(&enter_employee_id);
********************************************************
=> using OUTPUT parameters in procedure
CREATE OR REPLACE PROCEDURE getEmployeeSalaryById(emp_id IN number, sal OUT employees.salary%TYPE) IS
BEGIN
select salary into sal from employees where employee_id = emp_id;
--dbms_output.put_line('Employee '||employee1.employee_id||' '||employee1.first_name||' '||employee1.last_name||' '||employee1.salary);
END;
/
DECLARE
sal employees.salary%TYPE;
BEGIN
getEmployeeSalaryById(100,sal);
dbms_output.put_line('Employee Salary Rs: '||sal);
END;
/
********************************************************
=> calling procedure from another procedure
CREATE OR REPLACE PROCEDURE getEmployeeSalaryById(emp_id IN number, sal OUT employees.salary%TYPE) IS
BEGIN
select salary into sal from employees where employee_id = emp_id;
--dbms_output.put_line('Employee '||employee1.employee_id||' '||employee1.first_name||' '||employee1.last_name||' '||employee1.salary);
END;
/
CREATE OR REPLACE PROCEDURE callSalaryProcedure IS
sal employees.salary%TYPE;
BEGIN
getEmployeeSalaryById(&enter_employee_id, sal);
dbms_output.put_line('Employee Salary Rs: '||sal);
END;
/
EXEC callSalaryProcedure;
********************************************************
=> Procedure printing single result set
CREATE OR REPLACE PROCEDURE getEmployees(minimum_salary number) IS
cursor1 SYS_REFCURSOR;
employee1 employees%ROWTYPE;
BEGIN
OPEN cursor1 for select * from employees where salary = minimum_salary order by employee_id;
LOOP
FETCH cursor1 into employee1;
dbms_output.put_line(employee1.employee_id||' '||employee1.salary);
EXIT WHEN cursor1%NOTFOUND;
END LOOP;
CLOSE cursor1;
END;
/
EXEC getEmployees(9000);
********************************************************
### FUNCTIONS ### -> named sub programs in plsql which can be stored and reused later, should contain return value.
CREATE OR REPLACE FUNCTION calculateArea(radius number) RETURN number IS
result number;
PI constant number := 3.14;
BEGIN
result := PI*radius*radius;
return result;
END;
/
DECLARE
area number;
BEGIN
area := calculateArea(&enter_radius);
dbms_output.put_line('Arear of Circle : '||area);
END;
/
********************************************************
=> Function with IF condition
CREATE OR REPLACE FUNCTION calculateArea(radius number) RETURN number IS
result number;
PI constant number := 3.14;
BEGIN
result := PI*radius*radius;
return result;
END;
/
BEGIN
IF calculateArea(&enter_radius) > 50 THEN
dbms_output.put_line('Arear of Circle is greater than 50');
ELSE
dbms_output.put_line('Arear of Circle is greater than 50');
END IF;
END;
/
********************************************************
=> Function with SELECT query
CREATE OR REPLACE FUNCTION getSalaryByEmployeeId(emp_id number) RETURN number IS
sal employees.salary%TYPE;
BEGIN
select salary into sal from employees where employee_id = emp_id;
return sal;
END;
/
DECLARE
sal employees.salary%TYPE;
BEGIN
select getSalaryByEmployeeId(&enter_employee_id) into sal from dual;
dbms_output.put_line('Salary of the Employee is : '||sal);
END;
/
********************************************************
=> Function with Named Notation
CREATE OR REPLACE FUNCTION add_num(num1 number, num2 number DEFAULT 0,num3 number) RETURN number IS
BEGIN
dbms_output.put_line('number 1 : '||num1);
dbms_output.put_line('number 2 : '||num2);
dbms_output.put_line('number 3 : '||num3);
return num1+num2+num3;
END;
/
DECLARE
result_1 number;
BEGIN
result_1 := add_num(num1 => 1, num3 => 3);
dbms_output.put_line('Result is : '||result_1);
END;
/
********************************************************
### PACKAGES ###
=> creating Package Description -> contains only the declaration of the elements. if the description has cursor/procedure, then package body is mandatory.
else it's optional. they are visible public-outside the package also. if anything which is not defined in specification
but implemented in the package body, then it becomes private.
CREATE OR REPLACE PACKAGE emp_package1 IS
FUNCTION print_string RETURN varchar2;
PROCEDURE getEmployees(emp_id number);
END emp_package1;
********************************************************
=> creating Package Body -> contains implementation of the elements specified in the package specification.
create or replace PACKAGE BODY emp_package1 IS
FUNCTION print_string RETURN varchar2 IS
BEGIN
RETURN 'Hello World';
END print_string;
PROCEDURE getEmployees(emp_id number, sal OUT number) IS
BEGIN
select salary into sal from employees where employee_id = emp_id;
--dbms_output.put_line('Employee details : '||employee1.employee_id||' '||employee1.first_name||' '||employee1.last_name||' '||employee1.salary);
END getEmployees;
END emp_package1;
/
DECLARE
sal employees.salary%TYPE;
BEGIN
dbms_output.put_line(emp_package1.print_string);
emp_package1.getEmployees(&enter_employee_id, sal);
dbms_output.put_line('salary : '||sal);
END;
/
********************************************************
create or replace PACKAGE BODY emp_package1 IS
FUNCTION print_string RETURN varchar2 IS
BEGIN
RETURN 'Hello World';
END print_string;
PROCEDURE getEmployees(emp_id number) IS
employee1 employees%ROWTYPE;
BEGIN
select * into employee1 from employees where employee_id = emp_id;
dbms_output.put_line('Employee details : '||employee1.employee_id||' '||employee1.first_name||' '||employee1.last_name||' '||employee1.salary);
END getEmployees;
END emp_package1;
/
BEGIN
dbms_output.put_line(emp_package1.print_string);
emp_package1.getEmployees(&enter_employee_id);
END;
/
********************************************************
### TRIGGERS ### -> stored programs which are executed/triggered automatically based on the event happens
=> Synchronized table backup using TRIGGERS
CREATE OR REPLACE TRIGGER accounts_backup_trigger
BEFORE INSERT OR UPDATE OR DELETE ON accounts
FOR EACH ROW
ENABLE
BEGIN
IF INSERTING THEN
insert into accounts_backup values (:NEW.account_number, :NEW.holder_name,:NEW.balance,:NEW.credit_limit,:NEW.account_type,:NEW.eligiblity);
ELSIF DELETING THEN
delete from accounts_backup where account_number = :OLD.account_number;
ELSIF UPDATING THEN
update accounts_backup set holder_name = :NEW.holder_name, balance = :NEW.balance, credit_limit = :NEW.credit_limit, account_type = :NEW.account_type, eligiblity = :NEW.eligiblity where account_number = :OLD.account_number;
END IF;
END;
/
********************************************************
=> DDL trigger with schema auditing
CREATE OR REPLACE TRIGGER hr_audit_trigger
AFTER DDL ON SCHEMA
BEGIN
INSERT INTO schema_audit VALUES(sysdate, sys_context('USERENV','CURRENT_USER'),ora_dict_obj_type,ora_dict_obj_name,ora_sysevent);
END;
/
********************************************************
=> Database Event LOG'ON Trigger
CREATE OR REPLACE TRIGGER hr_logon_trigger
AFTER LOGON ON SCHEMA
BEGIN
INSERT INTO hr_event_audit VALUES(ora_sysevent,sysdate, TO_CHAR(sysdate,'HH24;MI:SS'),NULL,NULL);
COMMIT;
END;
/
********************************************************
=> Database Event LOG'OFF Trigger
CREATE OR REPLACE TRIGGER hr_logoff_trigger
BEFORE LOGOFF ON SCHEMA
BEGIN
INSERT INTO hr_event_audit VALUES(ora_sysevent,NULL,NULL,sysdate,TO_CHAR(sysdate,'HH24;MI:SS'));
COMMIT;
END;
/
********************************************************
=> START-UP Trigger
CREATE OR REPLACE TRIGGER startup_audit_trigger
AFTER STARTUP ON DATABASE
BEGIN
INSERT INTO startup_audit VALUES(ora_sysevent,sysdate,TO_CHAR(sysdate,'HH24:MI:SS'));
END;