select * from cat;
select table_name from cat;
select table_name,table_type from cat;
select table_name from cat where table_type='view';
select table_name from cat where table_type='table';

create table x(a number(8));
insert into x values(7);
insert into x values(6/5);
select a from x;
select * from x;
select x.* from x;
select t1.a from x t1;
select t1.a, t2.a from x t1, x t2;

create table y(a number(4), b number);
insert into y values (7,4);
insert into y values (7*(6-4),4);
select * from y;
select 1 from y;
select a,p.b from y p;
select a,y.b from y where y.a>10;
select a,y.b from y where y.a>10 and b<7;
select a,y.b from y where y.a>10 and b<=3;
select a,y.b from y where y.a>10 and b<=4;
select a,y.b from y where y.a>10 or b=4;
select a,y.b from y where (y.a>10);
select a,y.b from y where (y.a>10 and y.b<=4);
select a,y.b from y where ((y.a>10) and not b<=3);
select a,y.b from y where (y.a+3)*56>10 and not b<=3;
select a,y.b from y where (y.a+3)>10 and not b<=3;
select a,y.b from y where y.a>10 and not b<=3;
select a,y.b from y where not(y.a>10) and b<=3;
select a,y.b from y where not(y.a>10 and b<=3);
select a col1, y.b col2 from y;

select * from x,y;
select * from x,y where x.a=y.a;

drop table x;
create table x(a number(8));
insert into x values(7);
insert into x values(6/5);

select a from x;
select b from y;
select a from x union select b from y;
select * from x union select y.b from y;
insert into x values(4);
select a from x minus select b from y;

create table t(a number, b number, c number);
insert into t values (1,2,4);
insert into t values (1,2,5);
insert into t values (1,3,4);
select * from t;
select b from t where b>=5;
select b from t where b>=3;
select b from t where 2*a=1;
select b from t where 2*a=2;
select 10*a * 3, b+c c1 from t;
select 10*a * 3, b*c c1 from t;
select 1 from t union select c from t;

create table t1(a number, b number);
insert into t1 select * from y;
insert into t1 select a,b from y;
insert into t1 select b,a from y;
insert into t1 select a c1, b c2 from y union select a,a from x;
select * from t1;

create table s1 as select a c1 from y;
select * from s1;
select c1 from s1;

select * 
from x 
where a>3
and exists (
  select *
  from y
  where y.a=x.a);

select * 
from x 
where not exists (
  select 1
  from y
  where y.a>x.a);

select * 
from x 
where exists (
  select *
  from y
  where y.b<x.a);

select * from x where exists (select * from x where a >=7);
select * from x where exists (select * from x where a >=9);

create table t2 (a,b) as select a,a from x;
insert into t2 values (10,20);
select column_name from cols where table_name='t2';
select * from cols where table_name='t2';
select * from t2;

create table s as select * from y;
select * from s;
select b from s;
select * from cols where table_name = 's';

select table_name, column_name from cols order by table_name;
select table_name from cols order by column_name;
select table_name a1 from cols order by a1;
select table_name,column_name from cols order by 2;
select * from cols order by table_name+column_name;
select * from cols order by table_name,column_name;

select table_name, column_name from cols order by table_name desc;
select table_name from cols order by column_name desc;
select table_name,column_name from cols order by 2 desc;
select * from cols order by table_name desc,column_name asc;
select table_name from cols order by table_name desc;

#================================================================
# Aggregate
#================================================================
select max(a) from y;
select max(a)+min(b) from y;

select * from t;
drop table t1;
create table t1 as select c from t;
select * from t1;
drop table t1;
create table t1 as select c from t union (select b from t);
select * from t1;
drop table t1;
select 23*min(a/b) from t;

select b from t where exists 
  (select a from t where a=2
   union select b from t where b=3);

create table p(a char(5), b char(5), c number);
insert into p values ('12.56','def',15);
select * from p;
select to_number(a) from p;
select least(2*to_number(a),c*c) from p;
select greatest(2*to_number(a),c*c) from p;

select sum(b), sum(distinct b), sum(all b) from t;
select avg(b), avg(distinct b), avg(all b) from t;
select min(b), min(distinct b), min(all b) from t;
select max(b), max(distinct b), max(all b) from t;
select count(b) from t;

drop table t;
create table t(a char(4), b char(5), c char(3));
insert into t values ('abc','aab','a');
insert into t values ('aac','xd','aaa');
select * from t;

delete from y where a>10;
delete from y;

create sequence seq;
select seq.currval from dual;
select seq.nextval from dual;
create table p007(a integer, b integer);
insert into p007 values(1, seq.nextval);
insert into p007 values(2, seq.currval);
insert into p007 values(seq.nextval, seq.currval);
insert into p007 values(seq.nextval, seq.nextval);
insert into p007 values(seq.currval, seq.currval);
insert into p007 (select seq.nextval, seq.nextval from dual);
select * from p007;

create view v1 (c1) as select seq.nextval from dual;
create view v2 as select table_name from cat;

drop sequence seq;
create sequence seq;

drop table p;
savepoint a;
create table p(a number);
savepoint b;
insert into p values(6);
select * from p;
rollback to b;
select * from p;
rollback to a;
select * from p;

create table p(a number);
savepoint a;
insert into p values(2);
savepoint b;
insert into p values (5);
savepoint a;
rollback to a;
select * from p;
rollback to a;
rollback to b;
select * from p;

commit;
rollback to work;
rollback;

#================================================================
# Fehlermeldungen
#================================================================
create t(a char(88));

create view v 
  select *
  from y;

select a from x union select * from y;
select * from x union select 'grappa' from y;

select x.c from x;
select c from x;
select a from x,y;

insert into t values(1,2,3) /* wird noch ausgefuehrt! */;

create view v (c1) as select seq.nextval from seq;

#================================================================
# 1.Vordiplom F92, IIIA
#================================================================

create table person(pnum number,
                    name char(9),
                    lohn number,
                    age number,
                    anum number);
create table abteilung(anum number,name char(9));
create table chef(cnum number,unum number);

insert into person values(101,'Hans',40,22,10);
insert into person values(102,'Sonja',80,37,10);
insert into person values(119,'Fritz',30,18,10);
insert into person values(205,'Elsi',60,25,20);
insert into person values(307,'Peter',90,44,30);
insert into person values(309,'Carlo',80,33,30);
insert into abteilung values(10,'NETE');
insert into abteilung values(20,'ORNT');
insert into abteilung values(30,'GLAB');

insert into chef values(102,101);
insert into chef values(101,119);
insert into chef values(309,307);

#Aufgabe 1
#---------
select *
from person
where anum=10;

#Aufgabe 2
#---------
select name,age,lohn
from person
where age<30;

#Aufgabe 3
#---------
select person.name,lohn
from person,abteilung
where person.anum=abteilung.anum
and abteilung.name='GLAB';

select name,lohn
from person
where anum in
  (select anum
   from abteilung
   where name='GLAB');

#Aufgabe 4
#---------
select cnum
from chef,person
where unum=pnum
and name='Hans';

#Aufgabe 5
#---------
select unum
from chef,person
where cnum=pnum
and name='Carlo';

#Aufgabe 6
#---------
select p1.name,p1.age
from person p1
where exists
  (select *
   from person p2,chef
   where cnum=p1.pnum
   and p1.age<p2.age
   and p2.pnum=unum);

select name,age
from person
where pnum in
  (select cnum
   from chef
   where (unum,cnum) in
     (select x.pnum,y.pnum
      from person x, person y
      where y.age < x.age));

#================================================================
#Das Programm-Interface
#================================================================
quit;

sql_open_cursor(C), write(C), nl, fail.
sql_compile_statement(0,"select * from cat").
sql_exec_cursor(0,Num,Ret), format("~w ~w~n",[Num,Ret]), fail.
sql_fetch_row(0,Success), write(Success), nl, fail.
sql_get_column_string(0,0,Value,Ret), format("~s ~w~n",[Value,Ret]), fail.
sql_get_column_string(0,1,Value,Ret), format("~s ~w~n",[Value,Ret]), fail.
sql_fetch_row(0,Success), write(Success), nl, fail.
sql_get_column_string(0,0,Value,Ret), format("~s ~w~n",[Value,Ret]), fail.
sql_get_column_string(0,1,Value,Ret), format("~s ~w~n",[Value,Ret]), fail.
sql_close_cursor(0).

sql_open_cursor(C), write(C), nl, fail.
sql_compile_statement(0,"create table xx(a char)").
sql_exec_cursor(0,Num,Ret), format("~i ~w~n",[Num,Ret]), fail.
sql_close_cursor(0).

sql_open_cursor(C), write(C), nl, fail.
sql_compile_statement(0,"insert into xx values(:1)").
sql_put_var_string(0,1,"grappa").
sql_exec_cursor(0,Num,Ret), format("~w ~w~n",[Num,Ret]), fail.
sql_close_cursor(0).

sql_open_cursor(C), write(C), nl, fail.
sql_compile_statement(0,"select a from xx").
sql_exec_cursor(0,Num,Ret), format("~w ~w~n",[Num,Ret]), fail.
sql_fetch_row(0,Success), write(Success), nl, fail.
sql_get_column_string(0,0,Value,Ret), format("~s ~w~n",[Value,Ret]), fail.
sql_close_cursor(0).

sql_open_cursor(C), write(C), nl, fail.
sql_compile_statement(0,"select * from cat where table_name=:1").
sql_put_var_string(0,1,"cat").
sql_exec_cursor(0,Num,Ret), format("~w ~w~n",[Num,Ret]), fail.
sql_fetch_row(0,Success), write(Success), nl, fail.
sql_get_column_string(0,0,Value,Ret), format("~s ~w~n",[Value,Ret]), fail.
sql_get_column_string(0,1,Value,Ret), format("~s ~w~n",[Value,Ret]), fail.
sql_close_cursor(0).

#================================================================
#Abweichungen von ORACLE
#================================================================

#- any, all nicht implementiert
#- having nicht implementiert
#- Aggregatsfunktionen nicht vollsdtaendig
#- keine Stringoperationen verfuegbar (substring, ...)
#- kein Typ date

#================================================================
#Bugs
#================================================================
