如何在oracle中为MySQL中的last_insert_id()创建函数
我在MySQL中有一个过程,我想把它转换成Oracle过程,一切正常,但MySQL内置函数“last_insert_id()”会引发错误。有什么解决办法吗?我可以在oracle中为其创建函数吗如何在oracle中为MySQL中的last_insert_id()创建函数,mysql,sql,oracle,plsql,Mysql,Sql,Oracle,Plsql,我在MySQL中有一个过程,我想把它转换成Oracle过程,一切正常,但MySQL内置函数“last_insert_id()”会引发错误。有什么解决办法吗?我可以在oracle中为其创建函数吗 SELECT LAST_INSERT_ID(); 你不能在Oracle中执行类似的功能,它可以像在MySQL中一样运行。如果我错了,请纠正我 请看MySQL文档()中的注释 对于更改值的存储函数和触发器,值为 在函数或触发器结束时恢复,因此下面的语句 不会看到更改的值 重要的 如果使用单个inser
SELECT LAST_INSERT_ID();
你不能在Oracle中执行类似的功能,它可以像在MySQL中一样运行。如果我错了,请纠正我 请看MySQL文档()中的注释 对于更改值的存储函数和触发器,值为 在函数或触发器结束时恢复,因此下面的语句 不会看到更改的值 重要的 如果使用单个insert语句插入多行, LAST_INSERT_ID()返回为第一个插入的 只有一排。这样做的原因是为了使复制成为可能 很容易对其他服务器使用相同的INSERT语句 在Oracle中,自12c起,自动增量列就开始工作,它们基于顺序。如果有人调用sequence.NextVal,那么所有会话将只看到sequence的更改值。在MySQL中,行为是不同的(再次查看重要注意事项)
Oracle和MySQL的工作原理不同,尤其是在并行会话工作时。在Oracle中无法执行类似的功能,因为Oracle与MySQL的工作原理完全相同。如果我错了,请纠正我 请看MySQL文档()中的注释 对于更改值的存储函数和触发器,值为 在函数或触发器结束时恢复,因此下面的语句 不会看到更改的值 重要的 如果使用单个insert语句插入多行, LAST_INSERT_ID()返回为第一个插入的 只有一排。这样做的原因是为了使复制成为可能 很容易对其他服务器使用相同的INSERT语句 在Oracle中,自12c起,自动增量列就开始工作,它们基于顺序。如果有人调用sequence.NextVal,那么所有会话将只看到sequence的更改值。在MySQL中,行为是不同的(再次查看重要注意事项)
Oracle和MySQL的工作原理不同,尤其是在并行会话工作时。@krokodilko和@NicholasKrasnov告诉您如何使用returning子句 我的示例是,如果要获取刚刚插入的行的ID:
declare
l_id number;
begin
-- ONE ROW INSERT
l_id := someseq.nextval;
begin
insert into temp(id) values(l_id);
-- if insert fail then variable must be null
exception when others then l_id := null ;
end;
if l_id is not null then
-- do what you want RETURN this, or use for other statements
null;
-- here you have just now inserted ID in variable l_id
end if;
-- MULTIPLE INSERT
for some_data in (select * from some_joined_tables) loop
l_id := someseq.nextval;
begin
insert into temp(id) values(l_id);
exception when others then l_id := null ;
end;
if l_id is not null then
-- do what you want RETURN this, or use for other statements
-- here you have just now inserted ID in variable l_id
end if;
end loop;
-- HERE (after loop) YOU CAN RETURN LAST INSERTED VALUE of current transaction in variable l_id
end;
这只是代码的演示,我不会在服务器上编译它。@krokodilko和@NicholasKrasnov告诉你如何使用返回子句 我的示例是,如果要获取刚刚插入的行的ID:
declare
l_id number;
begin
-- ONE ROW INSERT
l_id := someseq.nextval;
begin
insert into temp(id) values(l_id);
-- if insert fail then variable must be null
exception when others then l_id := null ;
end;
if l_id is not null then
-- do what you want RETURN this, or use for other statements
null;
-- here you have just now inserted ID in variable l_id
end if;
-- MULTIPLE INSERT
for some_data in (select * from some_joined_tables) loop
l_id := someseq.nextval;
begin
insert into temp(id) values(l_id);
exception when others then l_id := null ;
end;
if l_id is not null then
-- do what you want RETURN this, or use for other statements
-- here you have just now inserted ID in variable l_id
end if;
end loop;
-- HERE (after loop) YOU CAN RETURN LAST INSERTED VALUE of current transaction in variable l_id
end;
这只是代码的演示,我不是在服务器上编译它在Oracle中搜索
returning子句的示例
,也检查一下这个链接Oracle和MySQL内部的工作原理不同。对MySQL有利的方法可能对Oracle不利。你想要这个函数的目的是什么?阅读有关oracle序列的内容,我认为它对你有帮助。@EvgeniyK.:我在MySQL中有一个过程,我已经将它转换为oracle过程。我在mysql中使用了last_insert_id()函数,但它会引发错误。所以我想在oracle中创建一个函数,它的行为和返回值类似于mysql last_insert_id()。@АаааааПаааааааа。这可能会有所帮助,但在某些情况下,由于并行会话,它的工作原理与MySQl不同。在Oracle中搜索returning子句的示例,也可以检查此链接Oracle和MySQl内部的工作原理不同。对MySQL有利的方法可能对Oracle不利。你想要这个函数的目的是什么?阅读有关oracle序列的内容,我认为它对你有帮助。@EvgeniyK.:我在MySQL中有一个过程,我已经将它转换为oracle过程。我在mysql中使用了last_insert_id()函数,但它会引发错误。所以我想在oracle中创建一个函数,它的行为和返回值类似于mysql last_insert_id()。@АаааааПаааааааа。这可能会有所帮助,但在某些情况下,由于并行会话,它的工作原理与MySQl不同。