-- Create table
create table ABIN
(
ID NVARCHAR2(50),
NAME VARCHAR2(30),
SID VARCHAR2(180),
CREATETIME NVARCHAR2(20)
)
tablespace USERS
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);
-- Create/Recreate indexes
create index AID on ABIN (ID)
tablespace USERS
pctfree 10
initrans 2
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);
create unique index ALLINDEX on ABIN (ID, NAME, SID)
tablespace USERS
pctfree 10
initrans 2
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);
create unique index USID on ABIN (SID)
tablespace USERS
pctfree 10
initrans 2
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);
-- Create table
create table LEE
(
ID NVARCHAR2(50) not null,
NAME VARCHAR2(30),
SID VARCHAR2(180)
)
tablespace USERS
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);
-- Create/Recreate primary, unique and foreign key constraints
alter table LEE
add constraint SSSSSID primary key (ID)
using index
tablespace USERS
pctfree 10
initrans 2
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);
-- Create table
create table LI
(
ID NVARCHAR2(50) not null,
NAME VARCHAR2(30),
SID VARCHAR2(180)
)
tablespace USERS
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);
-- Create/Recreate primary, unique and foreign key constraints
alter table LI
add constraint TTTID primary key (ID)
using index
tablespace USERS
pctfree 10
initrans 2
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);
create or replace procedure getProcess(tdate date,tid nvarchar2,tname varchar2)
as
mydate date;
v_sql varchar2(4000);
ssid varchar2(180):='';
cursor mycur is select * from lee where name=tname;
myrow lee%rowtype;
shecur sys_refcursor;
myexception EXCEPTION;
mysqlcode nvarchar2(50);
mysqlerrm varchar2(180);
jdate abin.createtime%type;
type hcur is ref cursor ;
hecur hcur;
herow li%rowtype;
begin
if(tid=1) then
begin
select to_date(to_char(sysdate,'yyyy-MM-dd HH24:mi:ss'),'yyyy-MM-dd HH24:mi:ss') into mydate from dual;
jdate:=to_char(mydate,'yyyy-MM-dd HH24:mi:ss');
dbms_output.put_line(jdate);
end;
if(to_char(tdate,'yyyy-MM-dd')=to_char(sysdate,'yyyy-MM-dd'))then
begin
dbms_output.put_line('this current tname is: '||tname);
select sid into ssid from lee where name=tname;
dbms_output.put_line('this current sid is: '||ssid);
if(ssid is not null)then
v_sql:='insert into abin (id,name,sid,createtime) values ('''||tid||''','''||tname||''','''||ssid||''','''||jdate||''')';
dbms_output.put_line('v_sql='||v_sql);
execute immediate v_sql;
dbms_output.put_line(' execute immediate v_sql='||v_sql);
commit;
end if;
end;
dbms_output.put_line('this input equals');
end if;
if(to_char(tdate,'yyyy-MM-dd')=to_char(sysdate-1,'yyyy-MM-dd'))then
begin
select sid into ssid from li where name=tname;
if(ssid is not null)then
v_sql:='insert into abin (id,name,sid,createtime)values ('''||tid||''','''||tname||''','''||ssid||''','''||jdate||''')';
execute immediate v_sql;
commit;
end if;
end;
dbms_output.put_line(' this input not equals ');
end if;
end if;
if(tid=2) then
begin
select to_date(to_char(sysdate,'yyyy-MM-dd HH24:mi:ss'),'yyyy-MM-dd HH24:mi:ss') into mydate from dual;
jdate:=to_char(mydate,'yyyy-MM-dd HH24:mi:ss');
dbms_output.put_line(jdate);
end;
dbms_output.put_line('tid :'||tid);
open mycur;
loop
fetch mycur into myrow;
ssid:=myrow.sid;
dbms_output.put_line('ssid :'||ssid);
if(ssid is not null)then
v_sql:='insert into abin (id,name,sid,createtime)values ('''||tid||''','''||tname||''','''||ssid||''','''||jdate||''')';
dbms_output.put_line('v_sql :'||v_sql);
execute immediate v_sql;
dbms_output.put_line('execute immediate v_sql :'||v_sql);
commit;
end if;
exit when mycur%NOTFOUND;
end loop;
if mycur%isopen then
close mycur;
end if;
end if;
if(tid=3) then
begin
select to_date(to_char(sysdate,'yyyy-MM-dd HH24:mi:ss'),'yyyy-MM-dd HH24:mi:ss') into mydate from dual;
jdate:=to_char(mydate,'yyyy-MM-dd HH24:mi:ss');
dbms_output.put_line(jdate);
end;
v_sql:='select * from li where name=:1';
open shecur for v_sql using tname;
loop
fetch shecur into myrow ;
ssid:=myrow.sid;
if(ssid is not null)then
v_sql:='insert into abin (id,name,sid,createtime)values ('''||tid||''','''||tname||''','''||ssid||''','''||jdate||''')';
execute immediate v_sql;
commit;
end if;
exit when shecur%NOTFOUND;
end loop;
if shecur%isopen then
close shecur;
end if;
end if;
if(tid=4)then
begin
select to_date(to_char(sysdate,'yyyy-MM-dd HH24:mi:ss'),'yyyy-MM-dd HH24:mi:ss') into mydate from dual;
jdate:=to_char(mydate,'yyyy-MM-dd HH24:mi:ss');
dbms_output.put_line(jdate);
end;
v_sql:='select * from li t where t.name=:1';
open hecur for v_sql using tname;
loop
fetch hecur into herow;
ssid:=herow.sid;
if(ssid is not null)then
v_sql:='insert into abin (id,name,sid,createtime)values ('''||tid||''','''||tname||''','''||ssid||''','''||jdate||''')';
execute immediate v_sql;
commit;
end if;
exit when hecur%NOTFOUND;
end loop;
if(hecur%isopen)then
close hecur;
end if;
end if;
if(tid=5)then
RAISE myexception;
end if;
exception
when myexception then
mysqlcode:=SQLCODE;
mysqlerrm:=SQLERRM;
begin
select to_date(to_char(sysdate,'yyyy-MM-dd HH24:mi:ss'),'yyyy-MM-dd HH24:mi:ss') into mydate from dual;
jdate:=to_char(mydate,'yyyy-MM-dd HH24:mi:ss');
dbms_output.put_line(jdate);
end;
begin
v_sql:='insert into abin(id,name,sid,createtime) values ('''||mysqlcode||''','''||tname||''','''||mysqlerrm||''','''||jdate||''')';
dbms_output.put_line('v_sql='||v_sql);
execute immediate v_sql;
commit;
end;
when others then
rollback;
end;
测试代码:
declare
mydate date:=to_date('2012-09-09','yyyy-MM-dd');
tid nvarchar2(50):='4';
tname varchar2(30):='abin';
begin
getProcess(mydate,tid,tname);
end;