Advertisement
Aniket_Goku

12 mu

Jan 29th, 2021 (edited)
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. --letter(deptno,deptname,lastletterno)
  2. create table letter
  3. (
  4. deptno number primary key,
  5. deptname varchar(20) not null,
  6. lastletterno number
  7. );
  8.  
  9.  
  10. --insertion
  11.  
  12. insert into letter values(10,'comp',101);
  13. insert into letter values(20,'ac',102);
  14. insert into letter values(30,'state',103);
  15.  
  16.  
  17.  
  18.  
  19. ---------------function
  20. create or replace function f1
  21. (dno in number,flag in varchar,sub_ref in varchar)
  22. return varchar
  23. is
  24. c varchar(20);
  25. temp number:=0;
  26. ch varchar2(40);
  27. cursor c1
  28. is
  29. select deptname
  30. from letter
  31. where deptno=dno;
  32. cursor y1
  33. is
  34. select to_char(sysdate,'yy-')
  35. from dual;
  36. cursor y2
  37. is
  38. select to_char(add_months(sysdate,12),'yy')
  39. from dual;
  40.  
  41. cursor lno
  42. is
  43. select lastletterno
  44. from letter
  45. where deptno=dno;
  46.  
  47. begin
  48. if dno=NULL then
  49. c:=0;
  50. else
  51. open c1;
  52. fetch c1 into c;
  53. ch:=concat(ch,c);
  54. ch:=concat(ch,'/');
  55. close c1;
  56.  
  57. end if;
  58.  
  59. if flag=NULL then
  60. c:=0;
  61.  
  62. elsif (flag='Y' or flag='y') then
  63. if(sub_ref=NULL) then
  64. c:=0;
  65. else
  66.  
  67. if (substr(sub_ref,1,1)='b' or substr(sub_ref,1,1)='B') then
  68. temp:=1;
  69. ch:=concat(ch,substr(sub_ref,2));
  70. ch:=concat(ch,'/');
  71. open y1;
  72. fetch y1 into c;
  73. ch:=concat(ch,c);
  74. close y1;
  75. open y2;
  76. fetch y2 into c;
  77. ch:=concat(ch,c);
  78. ch:=concat(ch,'/');
  79. close y2;
  80. elsif (substr(sub_ref,1,1)='a' or substr(sub_ref,1,1)='A') then
  81. temp:=1;
  82. open y1;
  83. fetch y1 into c;
  84. ch:=concat(ch,c);
  85. close y1;
  86. open y2;
  87. fetch y2 into c;
  88. ch:=concat(ch,c);
  89. ch:=concat(ch,'/');
  90. close y2;
  91. ch:=concat(ch,substr(sub_ref,2));
  92. ch:=concat(ch,'/');
  93.  
  94. else
  95. temp:=1;
  96. open y1;
  97. fetch y1 into c;
  98. ch:=concat(ch,c);
  99. close y1;
  100. open y2;
  101. fetch y2 into c;
  102. ch:=concat(ch,c);
  103. ch:=concat(ch,'/');
  104. close y2;
  105. end if;
  106. end if;
  107. end if;
  108. if temp=0 then
  109. ch:=concat(ch,substr(sub_ref,2));
  110. ch:=concat(ch,'/');
  111. end if;
  112. open lno;
  113. fetch lno into c;
  114.  
  115. update letter
  116. set lastletterno=lastletterno+1
  117. where deptno=dno;
  118. ch:=concat(ch,c);
  119.  
  120. close lno;
  121. return ch;
  122. end f1;
  123. /
  124. --
  125. begin
  126. dbms_output.put_line(f1(30,'Y',NULL));
  127. end;
  128. /
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement