if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[fn_test]'))
begin
   select 1
end if object_id(N'[dbo].[fn_test]') is  not  null
begin
   select 2
end 
请问这个2句sql语句(object_id)和 (select * from dbo.sysobjects where id = object_id())这个有区别吗

解决方案 »

  1.   

    判断的时候最好加一个type的限定,指定对象是表 函数 存储过程等,如果有相同名字,例如有fn_test的表及存储过程,那么这个判断不是很准确。
      

  2.   


    if exists 
    (select * from dbo.sysobjects where id = object_id(N'[dbo].[fn_test]'))--加type类型
    begin
       select 1end --两种写法没有区别