我现在有一张表,我想在插入和修改的时候对某一个字段进行修改。
具体如下:
1.字段A根据字段B的值和一个序列的值组合后自动生成。
当插入字段B的值是S的时候,在A中插入S001,其中001是序列产生的。
当插入字段B的值是K的时候,在A中插入K001,其中001是序列产生的。
就是说后三位由序列产生,现在要根据B的值来添加第一位。
我自己写的老错,请高手帮个忙!!
6 个解决方案
#1
create table test(a varchar2(10),b varchar2(10));
create sequence TEST_SEQ_S
minvalue 1
maxvalue 999999999999999999999999999
start with 1
increment by 1
cache 20;
create sequence TEST_SEQ_K
minvalue 1
maxvalue 999999999999999999999999999
start with 1
increment by 1
cache 20;
create or replace trigger test_trigger
before insert or update on test
for each row
declare
intTest_seq_s number;
intTest_seq_k number;
begin
if :new.b='S' then
select test_seq_s.nextval into intTest_seq_s from dual;
:new.a:='S' || lpad(intTest_seq_s,3,0);
elsif :new.b='K' then
select test_seq_k.nextval into intTest_seq_k from dual;
:new.a:='K' || lpad(intTest_seq_k,3,0);
end if;
end test_trigger;
begin
insert into test values('','S');
insert into test values('','K');
insert into test values('','S');
insert into test values('','K');
end;
执行后的数据
a b
S001 S
K001 K
S002 S
K002 K
看看是这个结果不
#2
应该不是限定只有s,k吧,只是新值的第一位,对吗?
#3
DROP TABLE tt;
create table tt(a varchar2(5),b varchar2(5));
DROP SEQUENCE seq_1;
create sequence seq_1;
CREATE OR REPLACE TRIGGER TRIG_TT
BEFORE INSERT OR UPDATE ON tt
FOR EACH ROW
DECLARE
seq_val NUMBER;
BEGIN
SELECT seq_1.nextval INTO seq_val FROM dual ;
:new.A := upper(:new.B) || ltrim(TO_CHAR(seq_val, '000'));
END;
/
BEGIN
FOR I IN 1 .. 5 LOOP
INSERT INTO TT
(B)
VALUES
(CASE WHEN DBMS_RANDOM.VALUE(1, 10)> 5 THEN 's' ELSE 'k' END);
END LOOP;
END;
/
SELECT * FROM tt;
输出:
1 S001 s
2 S002 s
3 K003 k
4 S004 s
5 S005 s
#4
先创建一个最大值为三位数的序列,循环使用:
create sequence seq start with 1 increment by 1 maxvalue 999 cycle;
在触发器里用这条语句即可:
select :new.b||to_char(seq.nextval,'fm099') into :new.a from dual;
create sequence seq start with 1 increment by 1 maxvalue 999 cycle;
在触发器里用这条语句即可:
select :new.b||to_char(seq.nextval,'fm099') into :new.a from dual;
#5
这个可以在程序段处理会更快一些!
#6
谢谢各位,已解决!
#1
create table test(a varchar2(10),b varchar2(10));
create sequence TEST_SEQ_S
minvalue 1
maxvalue 999999999999999999999999999
start with 1
increment by 1
cache 20;
create sequence TEST_SEQ_K
minvalue 1
maxvalue 999999999999999999999999999
start with 1
increment by 1
cache 20;
create or replace trigger test_trigger
before insert or update on test
for each row
declare
intTest_seq_s number;
intTest_seq_k number;
begin
if :new.b='S' then
select test_seq_s.nextval into intTest_seq_s from dual;
:new.a:='S' || lpad(intTest_seq_s,3,0);
elsif :new.b='K' then
select test_seq_k.nextval into intTest_seq_k from dual;
:new.a:='K' || lpad(intTest_seq_k,3,0);
end if;
end test_trigger;
begin
insert into test values('','S');
insert into test values('','K');
insert into test values('','S');
insert into test values('','K');
end;
执行后的数据
a b
S001 S
K001 K
S002 S
K002 K
看看是这个结果不
#2
应该不是限定只有s,k吧,只是新值的第一位,对吗?
#3
DROP TABLE tt;
create table tt(a varchar2(5),b varchar2(5));
DROP SEQUENCE seq_1;
create sequence seq_1;
CREATE OR REPLACE TRIGGER TRIG_TT
BEFORE INSERT OR UPDATE ON tt
FOR EACH ROW
DECLARE
seq_val NUMBER;
BEGIN
SELECT seq_1.nextval INTO seq_val FROM dual ;
:new.A := upper(:new.B) || ltrim(TO_CHAR(seq_val, '000'));
END;
/
BEGIN
FOR I IN 1 .. 5 LOOP
INSERT INTO TT
(B)
VALUES
(CASE WHEN DBMS_RANDOM.VALUE(1, 10)> 5 THEN 's' ELSE 'k' END);
END LOOP;
END;
/
SELECT * FROM tt;
输出:
1 S001 s
2 S002 s
3 K003 k
4 S004 s
5 S005 s
#4
先创建一个最大值为三位数的序列,循环使用:
create sequence seq start with 1 increment by 1 maxvalue 999 cycle;
在触发器里用这条语句即可:
select :new.b||to_char(seq.nextval,'fm099') into :new.a from dual;
create sequence seq start with 1 increment by 1 maxvalue 999 cycle;
在触发器里用这条语句即可:
select :new.b||to_char(seq.nextval,'fm099') into :new.a from dual;
#5
这个可以在程序段处理会更快一些!
#6
谢谢各位,已解决!