首页 > 让人难以相信的冷知识

function_exists function.identity

sql中exists的用法有哪些

exists的用法如下:

1、判断数据库是否存在

if exists(select*fromsysdatabaseswherename='数据库名')

dropdatabase[数据库名]

2、判断表是否存在

if not exists(select* from sysobjects where [name]='表名' and xtype='U')

begin

–这里创建表

end

3、判断存储过程是否存在

if exists(select*fromsysobjectswhereid= object_id(N'[存储过程名]') and OBJECTPROPERTY(id, N'IsProcedure')= 1)

dropprocedure[存储过程名]

4、判断视图是否存在

(1)SQL Server 2000

IF EXISTS(SELECT*FROMsysviewsWHEREobject_id='[dbo].[视图名]'

(2)SQL Server 2005

IF EXISTS(SELECT*FROMsys.viewsWHEREobject_id='[dbo].[视图名]'

5、判断函数是否存在

if exists(select*fromdbo.sysobjectswhereid= object_id(N'[dbo].[函数名]') and xtype in(N'FN', N'IF', N'TF'))

dropfunction[dbo].[函数名]

扩展资料

SQL的提升

1、复制表(只复制结构,源表名:a新表名:b)(Access可用)

法一:select* into b from a where 1<>1

法二:select top 0* into b from a

2、拷贝表(拷贝数据,源表名:a目标表名:b)(Access可用)

insert into b(x, y, z) select d,e,f from a;

3、跨数据库之间表的拷贝(具体数据使用绝对路径)(Access可用)

insert into b(x, y, z) select d,e,f from a in‘具体数据库’ where条件

例子:。.from b in'”&Server.MapPath(“.”&”\data.mdb”&”' where..

4、子查询(表名1:a表名2:b)

select a,b,c from a where a IN(select d from b或者: select a,b,c from a where a IN(1,2,3)

5、显示文章最后时间

select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b

参考资料:百度百科——SQL语句大全

…出现 “Warning: file_exists() [ function.file

这只是warning而已,不知道是不是php有些扩展或者模块没有开启导致。你可以在php的设置中,把错误提示的级别调低一点,这样的话,warning就不会出现了,一般来说,warning作为警告,并不影响程序的使用,把级别调低即可。

并且在网站在实际投入使用中,是肯定不能使用开发模型,否则很容易因为这些warning error影响前台显示。

function_exists()这个方法怎么理解

就是你打的那前三段

名称: function_exists()-找出含有指定函数的文件.

语法: string function_exists( string str, object ob);

字符串 function_exists(字符串 str,物件 ob);

用法:返回定义有函数 str之物件 ob的文件名称.如果函数 str是被定义在 ob所继承的物件中,返回的文件名称可能会与 file_name( ob)返回的不同.

如果物件 ob中没有定义函数 str则返回 0.

说简单点就是:检查指定的函数是否已经定义,也就是说括号里头必须填入字符串“”,这个字符串就是待检查的函数的名称。

oracle数据库中可以用 if exists 吗,我用为什么报错

对于Oracle中没有 if exists(…)的语法,目前有许多种解决方法,这里先分析常用的三种,推荐使用最后一种

第一种是最常用的,判断count(*)的值是否为零,如下

declare

v_cnt number;

begin

select count(*) into v_cnt from T_VIP where col=1;

if v_cnt= 0 then

dbms_output.put_line('无记录');

end if;

end;

首先这种写法让人感觉很奇怪,明明只需要知道表里有没有记录,却去统计了全表的记录数。

这种方式对于小表而言可以接受,一旦表记录很多的时候,性能问题就非常严重

因此有人就作了些修改,改成 select count(*) into v_cnt from T_VIP where col=1 and rownum=1

看起来似乎解决了性能问题,但是分析执行计划可以知道,实际上是一样的,不推荐使用。

第二种是所谓进攻式编程,不作预先判断,而是直接默认通过判断,然后使用 exception来捕获异常

比如我这里不判断表中是否有满足条件的记录,默认它有,如果没有就在异常中进行处理

declare

v_1 number;

begin

select vip_level into v_1 from T_VIP where 1=0;

exception

when no_data_found then

dbms_output.put_line('无记录');

end;

这种方式从性能上讲比第一种要好得多

不过首先它没办法适应所有的情况,如第一段代码它就没办法改造

其次这种代码看起来让人觉得好像是发生了异常,而不是正常运行,从而造成混乱,不推荐使用。

第三种是利用 Oracle原有的 Exists语法,如下

declare

v_cnt number;

begin

select count(*)

into v_cnt

from dual

where exists(select* from t_vip where col=1);

if v_cnt= 0 then

dbms_output.put_line('无记录');

end if;

end;

通过在语句的外面套上一层dual,来使用oracle原有的exists语法

虽然和第一种看起来类似,但分析执行计划可以知道,性能比以上两种都要好得多,与MSSQL的 if exists最接近,推荐使用。

可以把判断封装成一个函数以方便使用,代码如下

CREATE OR REPLACE FUNCTION EXISTS2(IN_SQL IN VARCHAR2)

RETURN NUMBER

IS

/**********************************************************

*使用示例

* begin

* if EXISTS2('select* from dual where 1=1')=1 then

* dbms_output.put_line('有记录');

* else

* dbms_output.put_line('无记录');

* end if;

* end;

*****************************************************************/

V_SQL VARCHAR2(4000);

V_CNT NUMBER(1);

BEGIN

V_SQL:='SELECT COUNT(*) FROM DUAL WHERE EXISTS('|| IN_SQL||')';

EXECUTE IMMEDIATE V_SQL INTO V_CNT;

RETURN(V_CNT);

END;

对于常用的insert判断还有更简单的写法,比如以下代码

if not exists(select* from table1 where id=1)

insert into table1 values(1,'a');

可以改写成

insert

when(not exists(select* from table1 where id=1)) then

into table1

select 1 as id,'a' as data from dual;

再比如以下的代码

if not exists(select* from table1 where id=2)

insert into table1 values(2,'b')

else

update table1 set data='b' where id=2;

可以改写成

merge into table1 his

using

(

select 2 as id,'b' as data from dual

) src

on(his.id=src.id)

when matched then

update set his.data=src.data where id=src.id

when not matched then

insert values(src.id,src.data);

这里附带说下,有人喜欢把count(*)写成count(列名),不推荐后一种,因为列名是需要额外的*作,去查询系统表来定位列信息

另外count(1)和count(*)没有差别,推荐使用count(*)直观明了

exists的用法

exists的用法如下:

1、判断数据库是否存在

if exists(select*fromsysdatabaseswherename='数据库名')

dropdatabase[数据库名]

2、判断表是否存在

if not exists(select* from sysobjects where [name]='表名' and xtype='U')

begin

–这里创建表

end

3、判断存储过程是否存在

if exists(select*fromsysobjectswhereid= object_id(N'[存储过程名]') and OBJECTPROPERTY(id, N'IsProcedure')= 1)

dropprocedure[存储过程名]

4、判断视图是否存在

(1)SQL Server 2000

IF EXISTS(SELECT*FROMsysviewsWHEREobject_id='[dbo].[视图名]'

(2)SQL Server 2005

IF EXISTS(SELECT*FROMsys.viewsWHEREobject_id='[dbo].[视图名]'

5、判断函数是否存在

if exists(select*fromdbo.sysobjectswhereid= object_id(N'[dbo].[函数名]') and xtype in(N'FN', N'IF', N'TF'))

dropfunction[dbo].[函数名]

扩展资料

SQL的提升

1、复制表(只复制结构,源表名:a新表名:b)(Access可用)

法一:select* into b from a where 1<>1

法二:select top 0* into b from a

2、拷贝表(拷贝数据,源表名:a目标表名:b)(Access可用)

insert into b(x, y, z) select d,e,f from a;

3、跨数据库之间表的拷贝(具体数据使用绝对路径)(Access可用)

insert into b(x, y, z) select d,e,f from a in‘具体数据库’ where条件

例子:。.from b in'”&Server.MapPath(“.”&”\data.mdb”&”' where..

4、子查询(表名1:a表名2:b)

select a,b,c from a where a IN(select d from b或者: select a,b,c from a where a IN(1,2,3)

5、显示文章最后时间

select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b

参考资料:百度百科——SQL语句大全

本文链接:http://www.okyx8.com/html/87964749.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。