Wednesday, August 29, 2007

desc emp;

desc dept;

select * from Dept;

select * from emp;

Name Null? Type

----------------------------------------- -------- ------------------

EMPNO NOT NULL NUMBER(4)

ENAME VARCHAR2(10)

JOB VARCHAR2(9)

MGR NUMBER(4)

HIREDATE DATE

SAL NUMBER(7,2)

COMM NUMBER(7,2)

DEPTNO NUMBER(2)

Name Null? Type

----------------------------------------- -------- -------------

DEPTNO NOT NULL NUMBER(2)

DNAME VARCHAR2(14)

LOC VARCHAR2(13)

select * from vNCre;

citations

1>

create table vNCre as select * from emp;

create table vNCre2 as select * from emp

where 1=2;

create table vNCre3 as select * from emp

where 1=1;

2>

select constraint_name,constraint_type from user_constraints

where table_name=’VNCRE’;

3>

alter table vNCre add constraint

Cemp primary key(EmpNo);

4>

select * from user_constraints

where table_name=’VNCRE’;

PREPARE


PL/SQL

Set serverout on;

---- t:=mod(no,10);

declare

no number(4);

re number(4):=0;

t number(4);

begin

no:=&no;

while (no>0)

loop

t:= no mod 10;

re:=((re*10)+t);

no:=trunc(no/10,2);

dbms_output.put_line(‘NO IS ‘ || no);

dbms_output.put_line(‘NO IS ‘ || re);

end loop;

dbms_output.put_line(‘NO IS >>‘ || re);

end;

/

REVERSE

Set serverout on;

declare

temp varchar2(10);

str varchar2(20):=’ABC’;

rev varchar2(20);

len number(4);

begin

dbms_output.put_line(LENGTH(str));

len:= LENGTH(str);

while (len>0)

loop

temp:=substr(str,len,1);

rev:=concat(rev,temp);

len:=len-1;

end loop;

dbms_output.put_line(rev);

end;

/

create or replace Procedure vNPro (str in varchar2,reve out varchar2) is

create or replace Procedure vNPro (str in varchar2) is

temp varchar2(20);

rev varchar2(20);

len number(2);

begin

len:=length(str);

dbms_output.put_line(‘LENGTH’ || len);

while (len>0)

loop

temp:=substr(str,len,1);

rev:=concat(rev,temp);

len:=len-1;

end loop;

dbms_output.put_line(rev);

end;

S REVERSE WITH PROCEDURE >>>>>>>>>>>>>>>>>>>>>>>>>

create or replace Procedure vNProOut (str in varchar,rev out varchar) is

temp varchar2(20);

len number(2);

begin

len:=length(str);

while(len>0)

loop

temp:=substr(str,len,1);

rev:=concat(rev,temp);

len:=len-1;

end loop;

end;

declare

tem varchar2(20);

begin

scott.vNProOut(‘vikaS’,tem);

dbms_output.put_line(‘REVER ‘ || tem);

end;

declare

str varchar2(10):=’ABC’;

temp varchar2 (10);

begin

temp:=substr(str,1,1);

dbms_output.put_line(‘TE’ || temp);

temp:=substr(str,2,1);

dbms_output.put_line(‘TE’ || temp);

temp:=substr(str,3,1);

dbms_output.put_line(‘TE’ || temp);

end;

dbms_output.put_line (ASCII(‘h’));

dbms_output.put_line (ASCII(str));

dbms_output.put_line (substr(str,2,4));

S REVERSE WITH FUNCTION >>>>>>>>>>>>>>>>>>>>>>>>>

Create or Replace Function FunIn (str in varchar)

Return varchar is

temp varchar2(20);

rev varchar2(20);

len number(10);

begin

len:=length(str);

while (len>0)

loop

temp:=substr(str,len,1);

rev:=concat(rev,temp);

len:=len-1;

end loop;

dbms_output.put_line(‘REV is ‘ || rev);

return rev;

end;

Create or Replace Trigger vnTrig

Before insert on Emp

for each row

Begin

Dbms_output.put_line(‘ majaa AYAAA!>>> Trigger badiya HAI’);

End;

Create or Replace Trigger vNTrig

Before insert on Emp

for each row

Begin

raise Err;

Exception when Err then

Dbms_output.put_line(‘ majaa AYAAA!>>> Trigger badiya HAI’);

End;

Insert into emp values (1001,’SARDAR’,’MD’,7566,sysdate,10000,20,20);

on Emp update on Emp delete

commit;

TRIGGER

Create or Replace Trigger vNTrigAcc

Before insert on vNAccount

for each row

declare

Err Exception;

Begin

dbms_output.put_line(‘ HI’);

if(:new.Bal<1) then

dbms_output.put_line (‘Records NOT INSERTED’);

rollback;

raise Err;

else

dbms_output.put_line (‘Records INSERTED’);

end if;

Exception when Err then

dbms_output.put_line(‘Plz Dep more than Rs. 1 >>>>’);

end;

delete from vNAccount;

Select * from vNAccount;

insert into vNAccount values (1,10);

declare

begin

dbms_output.put_line (scott.FunIn(‘vin’));

end;

CURSOR

Create table vNProduct (

ProdNo number(4) primary key,

Descr varchar2(4),

Qty number(4),

Rord number(4)

);

Create table vNOrder(

ProdNo number(4) references vNProduct(ProdNo),

Name varchar2(4),

Qty number(4)

);

insert into vNProduct values(1,’CAT’,10,20);

insert into vNProduct values(2,’MAT’,20,15);

select * from vNProduct;

select * from vNOrder;

commit;

Declare

Cursor CurIns is

Select * from vNProduct;

vRec vNProduct%rowtype;

Begin

Open CurIns;

Loop

Fetch CurIns into vRec;

If (vRec.Qty < vRec.ROrd ) then

Insert into vNOrder values (vRec.ProdNo,vRec.Descr,vRec.Qty);

Else

dbms_output.put_line (‘ WHAT THIS CUR SHOULD DO’);

End if;

Exit when CurIns %notfound;

End loop;

Close CurIns;

End;

/

PRIME NO’S >>>>>>>>>>>>>>>>>>>>>>>>>>

declare

flag numeric(1);

no numeric(2);

i numeric(1):=2;

begin

flag:=1;

no:=&FOR_PRIME;

while(i<no)

loop

if(no mod i=0) then

flag:=0;

i:=no;

else

i:=i+1;

flag:=1;

end if;

end loop;

if(flag=0) then

dbms_output.put_line(no || ‘ >>> NOT A PRIME’);

else

dbms_output.put_line(no || ‘ >>> its a PRIME’);

end if;

end;

PRIME NO’S USING PROC>>>>>>>>>>>>>>>>>>>>>>>

Create or Replace Procedure ProPrime (no in number) is

Create or Replace Procedure ProPrime (no in number,OutPt out varchar) is

div number(2):=2;

flag number(2):=0;

begin

while (div<no)

loop

if(mod(no,div)=0) then

flag:=1;

exit;

else

div:=div+1;

end if;

end loop;

if(flag=0) then

-- OutPt:=’PRIME’;

dbms_output.put_line(‘PRIME’);

else

-- OutPt:=’NOT PRIME’;

dbms_output.put_line(‘not PRIME’);

end if;

end;

declare

no number(2);

val varchar2(30);

begin

no:=&no;

-- scott.ProPrime (no,val);

scott.ProPrime (no);

--dbms_output.put_line(val);

end;

PRIME NO’S USING FUNC>>>>>>>>>>>>>>>>>>>>>>>

Create or Replace Procedure ProPrime (no in number) is

Create or Replace Function FuncPrime (no in number,OutPt out varchar)

Return varchar is

div number(2):=2;

flag number(2):=0;

begin

while (div<no)

loop

if(mod(no,div)=0) then

flag:=1;

exit;

else

div:=div+1;

end if;

end loop;

if(flag=0) then

OutPt:=’PRIME’;

dbms_output.put_line(‘>>>PRIME’);

else

OutPt:=’NOT PRIME’;

dbms_output.put_line(‘>>>not PRIME’);

end if;

return no;

end;

declare

no number(2);

val varchar2(30);

begin

no:=&no;

-- scott.ProPrime (no,val);

dbms_output.put_line(scott.FuncPrime(no,val));

dbms_output.put_line(val);

end;

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

CREATE table de ( c varchar2(2));

select * from de;

desc de;

Roll back;

Commit;

Savepoint st1;

CREATE table de1 ( c varchar2(2));

Roll back st1;

desc de1;

delete savepoint st1; -- how to

delete st1;

>>>>>>>>>>>>>

Declare

Cursor CurNor is

Select * from Emp;

N number(5):=2;

N2 number(5):=0;

Begin

Open CurNor;

-- Open CurNor;

dbms_output.put_line(N/N2);

Close CurNor;

Close CurNor;

Exception

when cursor_already_open OR invalid_cursor then

dbms_output.put_line(‘ Dude the Cursor is alreaDY OPENED OR CLOSED’);

when zero_divide then

dbms_output.put_line(‘ Dude the NO can'’t be divided with zerO %’);

End;

/

Assignment DISPLAY 2nd highest salary in ANOTHER WAY…

Create or Replace Function vNSec

Return varchar is

Cursor CurSec is

Select * from Emp order by sal desc;

vRec Emp %rowtype;

vMax Number(5):=1;

noRows number(5):=0;

Begin

Open CurSec;

Loop

Fetch CurSec into vRec;

dbms_output.put_line(‘NAME IS ‘||vRec.eName);

if(vMax=2) then

return vRec.eName;

end if;

vMax:=vMax+1;

noRows:= noRows+1;

exit when CurSec %notfound;

End Loop;

dbms_output.put_line(‘No of ROWS‘||noRows);

close CurSec;

End vNSec;

/

Declare

na varchar(20);

Begin

na:=Scott.vNSec;

dbms_output.put_line(‘NAME IS DE ‘ ||na);

End;

/

create table vNT (

RolNo number(2),

Nam varchar2(4),

DOB date);

Insert into vNT values (1,’tr’,’1-jan-2006’);

Insert into vNT values (2,’tr’,’2-jan-2006’);

Insert into vNT values (3,’tr’,’2-jan-2006’);

Insert into vNT values (4,’tr’,’2-jan-2006’);

Insert into vNT values (5,’tr’,’1-jan-2006’);

Insert into vNT values (6,’tr’,’1-jan-2006’);

Insert into vNT values (6,’tr’,’2-jan-2006’);

Insert into vNT values (7,’tr’,’17-oct-2005’);

Select * from vNT;

UPDATE vNT set nam=’adf’;

Delete from vNT;

Create or Replace Trigger vnTr

before insert on vNT

for each row

Begin

if(to_char(:new.DOB,’DD’)=’01’) then

dbms_output.put_line (‘ SORRY UR VALUE OF DATE SHOULD BE GREATER THAN ONE 1111111111 ’ || to_char(:new.DOB,’DD’));

rollback;

else

dbms_output.put_line (‘ nJOY’);

-- commit;

end if;

End;

Create or Replace Trigger vnTr

before insert on vNT

for each row

Begin

if(to_char(:new.DOB,’YY’)=’06’) then

dbms_output.put_line (‘ SORRY UR VALUE OF DATE SHOULD BE GREATER THAN ONE 1111111111 ’ || to_char(:new.DOB,’DD’));

rollback;

else

dbms_output.put_line (‘ nJOY’);

-- commit;

end if;

End;

Create or Replace Trigger vnTr1

before insert on vNT

for each row

Begin

if(to_char(:new.DOB,’DY’)=’SUN’) then

dbms_output.put_line (‘ SORRY UR VALUE OF DATE SHOULD BE GREATER THAN ONE 1111111111 ’ || to_char(:new.DOB,’DY’));

rollback;

else

dbms_output.put_line (‘ nJOY’);

-- commit;

end if;

End;

Create or Replace Trigger vnTr1

before insert or update or delete on vNT

for each row

Begin

dbms_output.put_line (‘ SORRY the table is TURNED TO HELL’);

-- rollback;

raise_application_error(-20222,’NOT ALLOWED’);

dbms_output.put_line (‘ nJOY’);

-- commit;

-- end if;

End;

Insert into vNT values (6,’tr’,’2-jan-2006’);

Insert into vNT values (7,’tr’,’17-oct-2005’);

Select * from vNT;

UPDATE vNT set nam=’adf’;

Delete from vNT;

desc emp;

desc dept;

select * from Dept;

select * from emp;


Name Null? Type

----------------------------------------- -------- ------------------

EMPNO NOT NULL NUMBER(4)

ENAME VARCHAR2(10)

JOB VARCHAR2(9)

MGR NUMBER(4)

HIREDATE DATE

SAL NUMBER(7,2)

COMM NUMBER(7,2)

DEPTNO NUMBER(2)

Name Null? Type

----------------------------------------- -------- -------------

DEPTNO NOT NULL NUMBER(2)

DNAME VARCHAR2(14)

LOC VARCHAR2(13)



1>

select distinct a.empno from emp a, emp b

where a.empno=b.mgr;


select distinct a.empno from emp a, emp b

where not a.mgr=b.empno


select distinct mgr from emp;


select distinct a.mgr from emp a, emp b

where a.mgr=b.empno




2> altered

Select * from Emp

where deptNo=(

select deptNo from dept where dName=’SALES’);


3>

select * from emp where not length(ename)>4;


4>

select * from dept where Dname like ‘S%K’;


4>

select * from dept where Dname like ‘A%G’;


5>

select * from emp where sal>3000;


select EMPNO, (sal+sal*.5) as “vinS” paY U” from emp where sal>3000;


select EMPNO, sal from emp where (sal+(sal*.2)) >3000;



6>

select * from emp where MGR=

(select empno from emp where ename=’JONES’ );


select a.EmpNo, a.ENAME, a.JOB, a.MGR, b.DNAME from emp a,

dept b where a.deptNo=b.DeptNo;


select ename from emp where

deptno=(select deptno from dept where DNAME=’SALES’);


select a.ename, b.dname from emp a, dept b where

a.deptno= 1;

(select deptno from dept where DNAME=’SALES’);


select a.ename, b.dname from emp a, dept b where

a.deptno=b.deptno

in

(select deptno from dept where DNAME=’SALES’);



7>


select a.ename, b.dname from emp a,dept b where

a.deptno=b.deptno

group by b.dname, a.ename

having dname=’SALES’;


8>


Select e.ename,d.dname,e.sal,e.comm from emp e, dept d

Where

e.deptno=d.deptno

group by

e.ename,d.dname,e.sal,e.comm

having

e.sal>2000 and e.sal<5000;


9>


Select e.ename,d.dname,e.sal,e.comm from emp e, dept d

Where

e.deptno=d.deptno

and e.sal>2000 and e.sal<5000 and d.loc=’CHICAGO’;


10>

select a.ename from emp a, emp b

where a.mgr=b.empno and a.sal>b.sal ;


11>

select a.* from emp a, emp b

where a.mgr=b.empno and a.sal>b.sal ;


12>


select e1.ename,d1.dname from emp e1, emp e2 , dept d1,dept d2

where e1.mgr=e2.empno and d1.deptno=d2.deptno and d2.dname=’SALES’





12>


select a.ename from emp a, emp b

where a.mgr=b.empno and

b.deptno in (select deptno from dept where dname=’SALES’)


13>

select * from emp where mgr is null;



select to_char(date_char('1/1/2001'),’mm’) from dual;



16>

select * from emp where

HIREDATE<=’31-dec-82’ and deptno in

(select deptno from dept where loc=’NEW YORK’ OR loc=’CHICAGO’);


select * from emp where

HIREDATE<=’31-dec-82’ and deptno in

(select deptno from dept where loc=’NEW YORK’ OR loc=’CHICAGO’)

and rownum <5;


select * from emp where

HIREDATE<=’31-dec-82’ and deptno in

(select deptno from dept where loc=’NEW YORK’ OR loc=’CHICAGO’)

and rownum <=5;



19;


declare

a number(3);

begin

a:=3;

--select * from emp where rownum< ‘’|| a;

dbms_output.put_line(‘select * from emp’ || a);


end;


select a.empNo, a.ename,b.ename from emp a, emp b

where a.mgr=b.empNo

AND

b.ename=’JONES’;


>41


select * from emp

WHERE rownum<=5;


Select * from emp

Where empNo in (


Select * from emp

Where (mod(sal,2))=0

);


;

junk


select max(sal) from emp


select sal from emp

order by sal desc



select * from (

select * from emp order by (sal) asc

)

where

rownum<=5


select sal from emp

where

rownum<=5

order by sal desc



where count(*)>=3



WHERE rownum<=5;



select * from emp where ename not like '__M%';


select * from emp where ename not like '__%';



select ename,empno from emp connect by prior empno=mgr

start with ename=’JONES’;


select ename from emp where hiredate like(‘__%’) = sal like (‘%__’);


select ename from emp where ename like(‘__%’) and ename like (‘%__’);