怎么用存储过程判断一个表是否存在?
CREATE proc IfTableExists
@TableName varchar(30),
@Exist int output
as
exec ( 'if exists (select * from dbo.sysobjects where id = object_id(N ' ' '
+@TableName+
' ' ') and OBJECTPROPERTY(id, N ' 'IsUserTable ' ') = 1)
set '+@Exist+ ' = 1
else set '+@Exist+ ' = 0 ')
GO
提示出错???
[解决办法]
--没必要用存储过程,用函数就可以。
create function f_table(@tablename sysname)
returns int
as
begin
declare @i int
select @i = 1 from sysobjects where id = object_id(@tablename) and type = 'u '
return isnull(@i,0)
end
go
select dbo.f_table( 'aaa ')