如何在Oracle数据库中编写函数 [英] How to write Function in Oracle Database
问题描述
亲爱的朋友们,
这里我是Oracle 11g数据库的新手,所以现在我要创建一个功能就像这样的功能
Dear Friends,
Here iam new to Oracle 11g Database so now iam going to create one Function that Function is like this
create or replace FUNCTION AUTO_INCRIMENT
(
Maxid_ NUMBER,
TableName_ VARCHAR2
) RETURN NUMBER AS
BEGIN
select max(Maxid_)+1 from tbl_TableName;
END AUTO_INCRIMENT;
但它没有显示错误:
我的想法是创建AutoIncrement,因为Oracle没有身份coloumn所以我创建一个函数,我想在插入特定表时调用函数。所以我想要知道如何创建AutoIncrement功能建议我
问候,
AnilKumar.D
But its not WOrking its showing error:
My Idea is to Create AutoIncrement because Oracle does'nt have identity coloumn so iam creating one Function and i want to call the Function when inserting the perticular table.So i want to know how to create the AutoIncrement function suggest me please
Regards,
AnilKumar.D
推荐答案
你的方法错了,Oracle不是SQLServer。
如果你有两个进程在他们获得相同ID的同时调用你的函数会导致应用程序崩溃。
使用序列代替:
You're having the wrong approach, Oracle isn't SQLServer.
If you have two processes that call your function at the same time they would get the same ID which would crash your application.
Use a Sequence instead:
CREATE
SEQUENCE MyID_SEQ
MINVALUE 1
MAXVALUE 999999
/
并在您的插入中使用它,如下所示:
and use it in your inserts like this:
INSERT INTO MyTable (MyID,Value)
(MyID_SEQ.Nextval,12)
这篇关于如何在Oracle数据库中编写函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!