♂♂♂♂♂oracle 怎么实现LAST_INSERT_ID()♀♀♀♀♀

不知-道人 2011-06-10 09:51:37
oracle 怎么实现LAST_INSERT_ID(mysql的函数())
我对这个函数不太了解
希望高手 给我详细解释一下 这个函数的作用 和 用法
在网上看到的有一些例子 可是解释的都不太清楚
在次希望高手把源码 贴一下
求求高手们了
...全文
586 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
304的的哥 2011-06-10
  • 打赏
  • 举报
回复
不懂mysql:
Important: If you insert multiple rows using a single INSERT statement,
LAST_INSERT_ID() returns the value generated for the first inserted row only.
不知-道人 2011-06-10
  • 打赏
  • 举报
回复
怎么没人啊
tangren 2011-06-10
  • 打赏
  • 举报
回复
oracle没有自增列,只能建一个序列来配合表来实现,
并且使用currval只能取到序列最后的值
SQL> create table t (id int, name varchar2(20));

表已创建。

SQL> create sequence t_seq start with 1;

序列已创建。

SQL> insert into t values(t_seq.nextval,'tom');

已创建 1 行。

SQL> select t_seq.currval from dual;

CURRVAL
----------
1

SQL> insert into t select t_seq.nextval,object_name from user_objects where rownum<=3;

已创建3行。

SQL> select t_seq.currval from dual;

CURRVAL
----------
4

SQL>
tangren 2011-06-10
  • 打赏
  • 举报
回复
这是取mysql当前连接最新执行生成自增列的值LAST_INSERT_ID。
如果一次批量插入多个值,取的仍然是第一个值。
mysql> create table t(id int auto_increment primary key,name varchar
Query OK, 0 rows affected (0.41 sec)

mysql> insert into t(name) values('tom');
Query OK, 1 row affected (0.03 sec)

mysql> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
| 1 |
+------------------+
1 row in set (0.00 sec)

mysql> insert into t(name) values('kyte');
Query OK, 1 row affected (0.03 sec)

mysql> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
| 2 |
+------------------+
1 row in set (0.00 sec)

mysql> insert into t(name) values('bob'),('white'),('jerry');
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql> select * from t;
+----+-------+
| id | name |
+----+-------+
| 1 | tom |
| 2 | kyte |
| 3 | bob |
| 4 | white |
| 5 | jerry |
+----+-------+
5 rows in set (0.00 sec)

mysql> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
| 3 |
+------------------+
1 row in set (0.00 sec)

mysql>

17,086

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧