delgal01na

Untitled

Jul 9th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.64 KB | None | 0 0
  1. /* PL/SQL EXAMPLE FOR LOOP. ADDITIONAL : "REVERSE" LOOP */
  2.  
  3. SET SERVEROUTPUT ON
  4.  
  5. DECLARE CTR NUMBER DEFAULT 0;
  6. BEGIN
  7. FOR I IN 1..6
  8. --FOR I IN REVERSE 1..6
  9. LOOP
  10. CTR:=CTR+2;
  11. DBMS_OUTPUT.PUT_LINE('CTR : '||CTR);
  12. --DBMS_OUTPUT.PUT_LINE('I : '||I);
  13. END LOOP;
  14. END;
  15. /
  16.  
  17. /*Q : COUNT NUMBER OF DIGITS */
  18.  
  19. DECLARE
  20. NUM INT DEFAULT '123456789';
  21. CTR INT DEFAULT '0';
  22. BEGIN
  23. NUM:=NVL('&NUM',NUM); -- NVL FUNC CHECKS FOR NULL VALUE AND REPLACES IT WITH THE 2ND ARGUMENT
  24. DBMS_OUTPUT.PUT_LINE('NUM : '|| NUM);
  25. WHILE NUM>0
  26. LOOP
  27. NUM:=FLOOR(NUM/10);
  28. CTR:=CTR+1;
  29. END LOOP;
  30. DBMS_OUTPUT.PUT_LINE('NO. OF DIGITS : '||CTR);
  31. END;
  32. /
  33.  
  34. /* PL/SQL EXAMPLES */
  35.  
  36. /* NOTE : SET SERVEROUTPUT ON (TO BE ABLE TO DISPLAY DOPL STATEMENTS) */
  37.  
  38. SET SERVEROUTPUT ON
  39.  
  40. DECLARE MARKS NUMBER DEFAULT 50;
  41. BEGIN
  42. MARKS:=&MARKS;
  43. IF MARKS >= 80 AND MARKS <= 100 THEN
  44. DBMS_OUTPUT.PUT_LINE('GRADE "A" : MARKS '||MARKS);
  45. ELSIF MARKS >= 70 AND MARKS <= 79 THEN
  46. DBMS_OUTPUT.PUT_LINE('GRADE "B" : MARKS '||MARKS);
  47. ELSIF MARKS >= 60 AND MARKS <= 69 THEN
  48. DBMS_OUTPUT.PUT_LINE('GRADE "C" : MARKS '||MARKS);
  49. ELSIF MARKS >= 41 AND MARKS <= 59 THEN
  50. DBMS_OUTPUT.PUT_LINE('GRADE "D" : MARKS '||MARKS);
  51. ELSE
  52. DBMS_OUTPUT.PUT_LINE('GRADE "FAIL" : MARKS '||MARKS);
  53. END IF;
  54. END;
  55. /
  56.  
  57.  
  58. /* PROCEDURE */
  59.  
  60. -- SYNTAX
  61. CREATE PROCEDURE PRCRTRV ( parameter )
  62. AS
  63. BEGIN
  64. ...
  65. END;
  66.  
  67. -- EXAMPLE (*OUT* MODE)
  68. CREATE PROCEDURE PRCRTRV ( MYUSN NUMBER, CLGCD OUT NUMBER )
  69. AS
  70. BEGIN
  71. SELECT CLG_CODE INTO CLGCD FROM STUDENT WHERE USN = MYUSN;
  72. END;
  73.  
  74. DECLARE
  75. USN NUMBER;
  76. CLGCODE NUMBER;
  77. BEGIN
  78. USN:=707
  79. PRCRTRV(USN, CLGCODE);
  80. DOPL(CLGCODE);
  81. END;
  82.  
  83. -- EXAMPLE (*IN OUT* MODE)
  84. CREATE OR REPLACE PROCEDURE PRCRTRV(PARAMTR IN OUT NUMBER)
  85. AS
  86. BEGIN
  87. SELECT CLG_CODE INTO PARAMTR FROM STUDENT WHERE USN = PARAMTR;
  88. END;
  89.  
  90. DECLARE NUM NUMBER;
  91. BEGIN
  92. NUM:=707;
  93. PRCRTRV(NUM);
  94. DOPL(NUM);
  95. END;
  96.  
  97.  
  98. ----------- DEFAULT STUDENT TABLE---------
  99. create table student(usn number(5), s_name varchar(20), clg_code number(5), marks number(4));
  100. insert into student values('001', 'abc', '111', '100');
  101. insert into student values('002', 'def', '112', '99');
  102. insert into student values('003', 'ghi', '113', '98');
  103. insert into student values('004', 'jkl', '111', '99');
  104.  
  105.  
  106.  
  107. --- Differentiating Age Groups---
  108.  
  109. 18-30
  110. 30-40
  111. 40-60
  112.  
  113.  
  114.  
  115. create table tab1(
  116. age number
  117. );
  118.  
  119.  
  120. create table tab2(
  121. age number
  122. );
  123.  
  124.  
  125. create table tab3(
  126. age number
  127. );
  128.  
  129.  
  130. create or replace procedure prcInsert(Age Number)
  131. as
  132. begin
  133. if Age>=10 and Age<30
  134. then
  135. insert into tab1 values(Age);
  136. elsif Age>=30 and Age<40
  137. then
  138. insert into tab2 values(Age);
  139. else
  140. insert into tab3 values(Age);
  141. end if;
  142. end;
  143.  
  144.  
  145.  
  146. ----------FUNCTION-----------
  147.  
  148. create or replace function converter(dollar number)
  149. return number
  150. as
  151. rupees number;
  152. begin
  153. rupees:= dollar*64;
  154. return rupees;
  155. end;
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162. ------------- CURSORS -----------------
  163.  
  164.  
  165. Cursor is a work area in pl/sql which is used by sql server used to store the result of a query. Each column value is pointed using pointer. You can independently manipulate cursor values. A bit about it’s working….. suppose u ask for a query stored in the server … at first a cursor consisting of query result is created in server…now the cursor is transferred to the client where again cursor is created and hence the result is displayed……
  166.  
  167. Cursors are of 2 types: implicit and explicit…….implicit cursors are created by oracle engine itself while explicit cursors are created by the users……cursors are generally used in such a case when a query returns more than one rows….normal pl/sql returning more than one rows givens error but using cursor this limitation can be avoided….so cursors are used….
  168.  
  169.  
  170. cursor attributes
  171.  
  172. %ISOPEN == returns true if ursor is open, false otherwise
  173. %FOUND == returns true if recod was fetched successfully, false otherwise
  174. %NOTFOUND == returns true if record was not fetched successfully, false otherwise
  175. %ROWCOUNT == returns number of records processed from the cursor.
  176.  
  177. Very important: Cursor can be controlled using following 3 control statements. They are Open, Fetch, Close…..open statement identifies the active set…i.e. query returned by select statement…close statement closes the cursor…and fetch statement fetches rows into the variables…Cursors can be made into use using cursor for loop and fetch statement…we will see the corresponding examples…
  178.  
  179. EXAMPLES
  180.  
  181.  
  182.  
  183. --EXAMPLE OF SQL%FOUND (IMPLICIT CURSORS)--
  184. begin
  185. update employee set salary=salary *0.15
  186. where emp_code = &emp_code;
  187. if sql%found then
  188. dbms_output.put_line('employee record modified successfully');
  189. else
  190. dbms_output.put_line('employee no does not exist');
  191. end if;
  192. end;
  193.  
  194.  
  195. --EXAMPLE FOR SQL%NOTFOUND (IMPLICIT CURSORS)--
  196. begin
  197. update employee set salary = salary*0.15 where emp_code = &emp_code;
  198. if sql%notfound then
  199. dbms_output.put_line('employee no . does not exist');
  200. else
  201. dbms_output.put_line('employee record modified successfully');
  202. end if;
  203. end;
  204.  
  205.  
  206. --EXAMPLE FOR SQL%ROWCOUNT (IMPLICIT CURSORS)--
  207. declare
  208. rows_affected char(4);
  209. begin
  210. update employee set salary = salary*0.15 where job='programmers';
  211. rows_affected := to_char(sql%rowcount);
  212. if sql%rowcount > 0 then
  213. dbms_output.put_line(rows_affected || 'employee records modified successfully');
  214. else
  215. dbms_output.put_line('There are no employees working as programmers');
  216. end if;
  217. end;
  218.  
  219.  
  220. Syntax of explicit cursor: Cursor cursorname is sql select statement;
  221. Syntax of fetch : fetch cursorname into variable1, variable2…;
  222. Syntax of close; close cursorname;
  223. Syntax of open cursor; open cursorname;
  224.  
  225.  
  226.  
  227. --EXPLICIT CURSOR EG--
  228. DECLARE
  229. CURSOR c1 is SELECT * FROM emp;
  230. str_empno emp.empno%type;
  231. str_ename emp.ename%type;
  232. str_job emp.job%type;
  233. str_mgr emp.mgr%type;
  234. str_hiredate emp.hiredate%type;
  235. str_sal emp.sal%type;
  236. str_comm emp.comm%type;
  237. str_deptno emp.deptno%type;
  238. rno number;
  239. BEGIN
  240. rno := &rno;
  241. FOR e_rec IN c1
  242. LOOP
  243. IF c1%rowcount = rno THEN
  244. DBMS_OUTPUT.PUT_LINE (str_empno || ' ' || str_ename || ' ' || str_job || ' ' || str_mgr || ' ' || str_hiredate || ' ' || str_sal || ' ' || str_comm || ' ' || str_deptno);
  245. END IF;
  246. END LOOP;
  247. END;
  248.  
  249.  
  250. --ANOTHER EG DISPLAYING VALUE OF A TABLE--
  251. DECLARE
  252. CURSOR c1 IS SELECT * FROM emp;
  253. e_rec emp%rowtype;
  254. BEGIN
  255. OPEN c1;
  256. LOOP
  257. FETCH c1 INTO e_rec;
  258. DBMS_OUTPUT.PUT_LINE('Number: ' || ' ' || e_rec.empno);
  259. DBMS_OUTPUT.PUT_LINE('Name : ' || ' ' || e_rec.ename);
  260. DBMS_OUTPUT.PUT_LINE('Salary: ' || ' ' || e_rec.sal);
  261. EXIT WHEN c1%NOTFOUND;
  262. END LOOP;
  263. CLOSE c1;
  264. END;
  265.  
  266.  
  267. -- Display details of Highest 10 salary paid employee--
  268.  
  269. DECLARE
  270. CURSOR c1 IS SELECT * FROM emp ORDER BY sal DESC;
  271. e_rec emp%rowtype;
  272. BEGIN
  273. FOR e_rec IN c1
  274. LOOP
  275. DBMS_OUTPUT.PUT_LINE('Number: ' || ' ' || e_rec.empno);
  276. DBMS_OUTPUT.PUT_LINE('Name : ' || ' ' || e_rec.ename);
  277. DBMS_OUTPUT.PUT_LINE('Salary: ' || ' ' || e_rec.sal);
  278. EXIT WHEN c1%ROWCOUNT >= 10;
  279. END LOOP;
  280. END;
  281.  
  282.  
  283. -- EXAMPLE OF CURSOR FOR LOOP
  284. declare cursor c1 is select * from somdutt;
  285. begin
  286. for outvariable in c1
  287. loop
  288. exit when c1%notfound;
  289. if outvariable.age < 21 then
  290. dbms_output.put_line(outvariable.age || ' ' || outvariable.name);
  291. end if;
  292. end loop;
  293. end;
  294.  
  295.  
  296. --ref STRONG CURSORS--
  297. DECLARE
  298. TYPE ecursor IS REF CURSOR RETURN emp%ROWTYPE;
  299. ecur ecursor;
  300. e_rec emp%ROWTYPE;
  301. dn NUMBER;
  302. BEGIN
  303. dn := &deptno;
  304. OPEN ecur FOR SELECT * FROM emp WHERE deptno = dn;
  305. FOR e_rec IN ecur
  306. LOOP
  307. DBMS_OUTPUT.PUT_LINE ('Employee No : ' || e_rec.empno);
  308. DBMS_OUTPUT.PUT_LINE ('Employee Salary: ' || e_rec.salary);
  309. END LOOP;
  310. END;
  311.  
  312.  
  313. --REF WEAK CURSORS
  314. DECLARE
  315. TYPE tcursor IS REF CURSOR;
  316. tcur tcursor;
  317. e1 emp%ROWTYPE;
  318. d1 dept%ROWTYPE;
  319. tname VARCHAR2(20);
  320. BEGIN
  321. tname := &tablename;
  322. IF tname = 'emp' THEN
  323. OPEN tcur FOR SELECT * FORM emp;
  324. DBMS_OUTPUT.PUT_LINE ('Emp table opened.');
  325. close tcur;
  326. DBMS_OUTPUT.PUT_LINE ('Emp table closed.');
  327. ELSE IF tname = 'dept' THEN
  328. OPEN tcur FOR SELECT * FROM dept;
  329. DBMS_OUTPUT.PUT_LINE ('Dept table opened.');
  330. close tcur;
  331. DBMS_OUTPUT.PUT_LINE ('Emp table closed.');
  332. ELSE
  333. RAISE_APPLICATION_ERROR (-20004, 'Table name is wrong');
  334. END IF;
  335. END;
  336.  
  337.  
  338. --CURSOR FOR LOOP WITH PARAMETERS--
  339. Declare
  340. Cursor c1(Dno number) is select * from emp where deptno = dno;
  341. begin
  342. for empree in c1(10) loop;
  343. dbms_output.put_line(empree.ename);
  344. end loop;
  345. end;
  346.  
  347.  
  348.  
  349. DECLARE CURSOR C1 IS SELECT * FROM STUDENT where usn > 3;
  350. ABC STUDENT%ROWTYPE;
  351. BEGIN
  352. for abc in c1
  353. LOOP
  354. DBMS_OUTPUT.PUT_LINE(ABC.s_name);
  355. end loop;
  356. END;
  357.  
  358.  
  359. DECLARE TYPE T IS
  360. TABLE OF STUDENT%ROWTYPE;
  361. TEST1 T;
  362. BEGIN
  363. SELECT * BULK COLLECT INTO TEST1 FROM STUDENT WHERE USN > 3;
  364. DBMS_OUTPUT.PUT_LINE(TEST1.S_NAME||SQL%ROWCOUNT);
  365. END;
Add Comment
Please, Sign In to add comment