如何在Oracle数据库中编写函数 [英] How to write Function in Oracle Database

查看:397
本文介绍了如何在Oracle数据库中编写函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友们,



这里我是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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆