简体   繁体   English

如何在Oracle中按顺序插入2个查询?

[英]How to insert 2 queries with sequence in oracle?

I have two queries: 我有两个查询:

INSERT INTO CLASS
(TEACHER_ID, CLASS,)
VALUES (:i_teacher_id, :i_class)
INSERT INTO TEACHER_INFO
(TEACHER_ID, TEACHER_NAME) VALUES (:i_teacher_id, :i_teacher_name)

teacher_id is incremented by sequence like this: t0001, t0002, t0003 ...etc Teacher_id按如下顺序递增:t0001,t0002,t0003 ... etc

Then I tried to do it: 然后我尝试这样做:

INSERT INTO TEACHER_INFO
(TEACHER_ID, TEACHER_NAME) VALUES (teacher_seq.nextval, :i_teacher_name)

But sequence give me just "4" not t0004 但是序列给我的只是“ 4”而不是t0004

If teacher_id must be in this format, and must be constructed as part of the insert statement, you could do this: 如果Teacher_id必须采用这种格式,并且必须作为insert语句的一部分进行构造,则可以执行以下操作:

INSERT INTO TEACHER_INFO 
(TEACHER_ID, TEACHER_NAME) 
VALUES ('t'||LPAD(teacher_seq.nextval, 4, '0'), :i_teacher_name)

That is, add the initial "t", then append the next sequence number after padding it out to 4 digits with zeroes. 也就是说,添加初始的“ t”,然后将下一个序列号填充到带有零的4位数字之后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM