解决方案 »
- 存储过程编写问题
- 我的是虚拟机装的oracl 10g版本的,昨天还正常运行现在就不能启动该服务了
- 请问有哪些比较好的oracle的书
- 关于oracle11g(win2003)内存管理的问题!
- 请问如何查看ORACLE数据库执行的所有SQL语句?
- 如何将Cursor返回到前端语言(C#, VB, Java之类)中去.
- 服务器限制端口了,只有80,1521开放,程序可以连接到ORALCE,但是我PL/DEV,却连接不了ORALCE,提示没有监听器。
- 讨论 设计数据库时 代码名称是用 汉语拼音 还是该用 英文 代表更好?!
- windows8.1为什么安装不上JDK 7u67
- oracle存储过程编译错误,返回结果信息
- 如何通过sql语句得到一年当中每周一、每周五的日期?
- oracle 热备份需要备份哪些文件?
或者是在应用做插入之前,自己拼装好不过就是需要先根据A表先确定一下顺序,然后在进行拼装。而且这里还有不明的一点,如果顺序变化以后,B表里的以前的数据的jcbh要变吗?
before insert
on b
for each row
declare
v_isbn number;
v_bc number;
v_yc number;
begin
select isbn,bc,yc into v_isbn,v_bc,v_yc from(
select max(decode(colname,'isbn',cx))isbn,
max(decode(colname,'bc',cx))bc,
max(decode(colname,'yc',cx))yc from a);
if v_isbn=1 then
if v_bc=2 then
:new.jcbh:=:new.isbn||:new.bc||:new.yc||to_char(sysdate,'YYYYMMDD');
dbms_output.put_line(:new.isbn||:new.bc||:new.yc||to_char(sysdate,'YYYYMMDD'));
else
:new.jcbh:=:new.isbn||:new.yc||:new.bc||to_char(sysdate,'YYYYMMDD');
dbms_output.put_line(:new.isbn||:new.yc||:new.bc||to_char(sysdate,'YYYYMMDD'));
end if;
elsif v_isbn=2 then
if v_bc=1 then
:new.jcbh:=:new.bc||:new.isbn||:new.yc||to_char(sysdate,'YYYYMMDD');
dbms_output.put_line(:new.bc||:new.isbn||:new.yc||to_char(sysdate,'YYYYMMDD'));
else
:new.jcbh:=:new.yc||:new.isbn||:new.bc||to_char(sysdate,'YYYYMMDD');
dbms_output.put_line(:new.yc||:new.isbn||:new.bc||to_char(sysdate,'YYYYMMDD'));
end if;
else
if v_bc=1 then
:new.jcbh:=:new.bc||:new.yc||:new.isbn||to_char(sysdate,'YYYYMMDD');
dbms_output.put_line(:new.bc||:new.yc||:new.isbn||to_char(sysdate,'YYYYMMDD'));
else
:new.jcbh:=:new.yc||:new.bc||:new.isbn||to_char(sysdate,'YYYYMMDD');
dbms_output.put_line(:new.yc||:new.bc||:new.isbn||to_char(sysdate,'YYYYMMDD'));
end if;
end if;
end before_b_insert;
ISBN
版次(bc)
印次(yc)
条形码(barcode)
教材学科(jcxk)
教材类别(jclb)
出版社编码(cbsbh)
出版日期(cbrq)另外
select isbn,bc,yc into v_isbn,v_bc,v_yc from(
select max(decode(colname,'isbn',cx))isbn,
max(decode(colname,'bc',cx))bc,
max(decode(colname,'yc',cx))yc from a);
里不能直接使用Order by 吗?
after insert
on b
declare
v_1 a.colname%type;
v_2 a.colname%type;
v_3 a.colname%type;
begin
select max(decode(cx,1,colname)),
max(decode(cx,2,colname)),
max(decode(cx,3,colname)) into v_1,v_2,v_3 from a;
execute immediate 'update b set jcbh='||v_1||'||'||v_2||'||'||v_3||'||to_char(sysdate,''YYYYMMDD'') where jcbh is null';
end before_b_insert;
这个例子中的项目个数为3,如果有多个,自己添加上去试试
你可以在触发器中定义N个参数(n是可能取到得最大项目数)
就OK了。用户没选的项目,值会为空,不影响结果
行级的每插入一行前触发,插入jcbh值
下面的那段在插入记录后触发,对jcbh为空的记录进行修改赋jcbh值
忘了改名称了,应该改为after_b_insert,避免误解
你可以加一句
jcbh=substr('||v_1||'||'||v_2||'||'||v_3||'||to_char(sysdate,''YYYYMMDD''),1,23)
可以设个字符串,根据colname里的值设定变量的个数。
待会我把修改后的代码贴上来
create or replace trigger before_b_insert
after insert
on b
declare
v_1 a.colname%type;
v_2 a.colname%type;
v_3 a.colname%type;
v_4 a.colname%type;
v_5 a.colname%type;
v_6 a.colname%type;
v_7 a.colname%type;
v_8 a.colname%type;
v_str varchar2(200):='v_1';
v_str1 varchar2(200):='v_1';
col_str varchar2(2000):='max(decode(cx,1,colname))';
sqlstr varchar2(2000);
v_count number;
begin
select max(decode(cx,1,colname)),
max(decode(cx,2,colname)),
max(decode(cx,3,colname)),
max(decode(cx,4,colname)),
max(decode(cx,5,colname)),
max(decode(cx,6,colname)),
max(decode(cx,7,colname)),
max(decode(cx,8,colname)) into
v_1,v_2,v_3,v_4,v_5,v_6,v_7,v_8 from a;
if v_1 is null then v_1:=''''''; end if;
if v_2 is null then v_2:=''''''; end if;
if v_3 is null then v_3:=''''''; end if;
if v_4 is null then v_4:=''''''; end if;
if v_5 is null then v_5:=''''''; end if;
if v_6 is null then v_6:=''''''; end if;
if v_7 is null then v_7:=''''''; end if;
if v_8 is null then v_8:=''''''; end if;
sqlstr:= 'update b set jcbh='||v_1||'||'||v_2||'||'||v_3||'||'||v_4||'||'
||v_5||'||'||v_6||'||'||v_7||'||'||v_8||'||to_char(sysdate,''YYYYMMDD'') where jcbh is null'; execute immediate sqlstr;
end before_b_insert;
v_str1 varchar2(200):='v_1';
col_str varchar2(2000):='max(decode(cx,1,colname))';
v_count number;
这几个是测试用的变量,可以从declare中删掉
drop trigger before_b_insert;create or replace trigger after_b_insert
after insert
on b
declare
v_1 a.colname%type;
v_2 a.colname%type;
v_3 a.colname%type;
v_4 a.colname%type;
v_5 a.colname%type;
v_6 a.colname%type;
v_7 a.colname%type;
v_8 a.colname%type;
sqlstr varchar2(2000);
begin
select max(decode(cx,1,colname)),
max(decode(cx,2,colname)),
max(decode(cx,3,colname)),
max(decode(cx,4,colname)),
max(decode(cx,5,colname)),
max(decode(cx,6,colname)),
max(decode(cx,7,colname)),
max(decode(cx,8,colname)) into
v_1,v_2,v_3,v_4,v_5,v_6,v_7,v_8 from a;
if v_1 is null then v_1:=''''''; end if;
if v_2 is null then v_2:=''''''; end if;
if v_3 is null then v_3:=''''''; end if;
if v_4 is null then v_4:=''''''; end if;
if v_5 is null then v_5:=''''''; end if;
if v_6 is null then v_6:=''''''; end if;
if v_7 is null then v_7:=''''''; end if;
if v_8 is null then v_8:=''''''; end if;
sqlstr:= 'update b set jcbh=substr('||v_1||'||'||v_2||'||'||v_3||'||'||v_4||'||'
||v_5||'||'||v_6||'||'||v_7||'||'||v_8||',1,17)||to_char(sysdate,''YYYYMMDD'') where jcbh is null'; execute immediate sqlstr;
end after_b_insert;
ORA-06512: 在"FREEMAN.TRIGGER_ON_JCXX", line 33
ORA-04088: 触发器 'FREEMAN.TRIGGER_ON_JCXX' 执行过程中出错
表的列名看有没有问题!
create or replace function ft
return varchar2
is
type cur is ref cursor;
c cur;
vt varchar2(100);
v_str varchar2(1000);
begin
open c for select col2 from a order by col1;
loop
fetch c into vt;
exit when c%notfound;
v_str := v_str||vt||'||';
dbms_output.put_line('v_str='||v_str);
end loop;
return(substr(v_str,1,length(v_str)-2));
end ft;2.创建触发器;
create or replace trigger tr_a
before insert on b
for each row
declare
v_sql varchar2(1000);
begin
v_sql := 'update b set jcbh='||ft||'||to_char(sysdate,''YYYYMMDD'') where jcbh is null';
execute immediate v_sql;
end tr_a;