解决方案 »
- oracle中使用union连接生成视图问题
- 我的意思是跟sql server 一样通过存储过程获取一个表,然后通过vb.net 获取此存储过程的表数据,我的存储过程如何做,vb.net 如何调用?
- oracle修改问题
- 装了Oracle9i客户端精简版能不能装Oracle服务器?
- 对于大数据量的表有什么好的策略可以解决性能问题?
- 关于Proc中动态SQL语句的PREPARE问题 --解决办法
- 《续二》各位高手帮我分析几道题,据说是某个公司的笔试题目。(感激!回复既有分数)
- 由于当前驱动器的空间不够用了,能不能把表空间建在另外的驱动器上?
- 在SQLPlus中怎么显示存储过程的语句并编辑它?
- 我的这个存储过程报pls00103:Encountered the symbol "b_cur " when exception one of the fol
- 如何通过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;