判断一个数据是否存在于一个表中,Oracle中写自定义函数

create or replace function isExist(data in DataTypes)  --DataTypes 为表中该数据的类型
return Number
is
v_flag number(2);
v_data [DataTypes]; --表中数据的类型
begin
select data into v_data from table_name where ....;
if v_data not null then
v_falg := 1;
else
v_flag :=0;
end if;
return v_falg;
Exception
when DATA_NOT_FOUND then
v_falg :=0;
return v_falg;
when OTHERS then
v_flag := -1;
return v_falg;
end;

--返回0不是该数据不存在
--返回1表示该数据存在
--返回-1表示sql语句有问题
--可能不需要if判断,但为了保险,加了一句,可能是多余的代码。
原文地址:https://www.cnblogs.com/xinxin1994/p/4996171.html