我想,需要为每个类别建一个seq 然后插入的时候判断是用哪一个seq.另外还可以根据后面的类别select max(id) from tablename where type='22'选出最大值.+1然后插入.
也可以不用建seq。 方法: 求出某种type的记录最大数+1,按一定格式用TO_CHAR把它转成字符,如需定长4位字符 SQL> select '22'||to_char(count(id)+1,'fm0000') from a where type='22';最好以上语句写在程序中,将查询结果赋给变量id_type,插入数据时id的值就是id_type 试试看
假定原来有表TAB1,字段NAME,TYPE NAME(VARCHAR2),TYPE(NUMBER) 要插入表TAB2,字段ID,NAME,TYPE insert into tab2 select a.type*100+a.rownum-b.row_min,a.name,a.type from ( select name,type,rownum rowid from tab1 order by type) a, (select type,min(row_id) row_min from (select name,type,rownum row_id from (select name,type from tab1 order by type) ) group by type ) b where a.type=b.type; 这样应该可以。
多谢楼上,我自己搞定了,根据楼上各位提供的例子。刚才三次回复满了,没法说明,没法结帖。:( 我用的好像没这么麻烦, insert into test (ucode, name, utype) values ((select TO_CHAR(substr(max(Ucode), -2)+1, 'fm000') from test where utype='a32'), 'www', 'a32'); 一句搞定:) 表结构是: ucode name utype 003 ddd a32 004 sss a32 ...... 本来想用count的,后来发现要是中间的给删了就麻烦了~ 好了,没事了,多谢各位帮忙~:)
如果只有有限的几类,可以对应每类建立一个seq
在insert触发器中根据类别的不同来创建id号。
就是因为这次不能用序列了~
snowy_howe(天下有雪):
那个“类别”很多~没准多少
方法:
求出某种type的记录最大数+1,按一定格式用TO_CHAR把它转成字符,如需定长4位字符
SQL> select '22'||to_char(count(id)+1,'fm0000') from a where type='22';最好以上语句写在程序中,将查询结果赋给变量id_type,插入数据时id的值就是id_type
试试看
NAME(VARCHAR2),TYPE(NUMBER)
要插入表TAB2,字段ID,NAME,TYPE
insert into tab2
select a.type*100+a.rownum-b.row_min,a.name,a.type
from ( select name,type,rownum rowid from tab1 order by type) a,
(select type,min(row_id) row_min
from (select name,type,rownum row_id
from (select name,type from tab1 order by type)
) group by type
) b
where a.type=b.type;
这样应该可以。
我用的好像没这么麻烦,
insert into test (ucode, name, utype) values
((select TO_CHAR(substr(max(Ucode), -2)+1, 'fm000') from test where utype='a32'), 'www', 'a32');
一句搞定:)
表结构是:
ucode name utype
003 ddd a32
004 sss a32
......
本来想用count的,后来发现要是中间的给删了就麻烦了~
好了,没事了,多谢各位帮忙~:)