Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Function To Test For Table Doesn't Work
Message
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Miscellaneous
Thread ID:
01328338
Message ID:
01328412
Views:
10
This seems to work consistently now:
ALTER FUNCTION dbo.fnTableExists
	(@sTableName VARCHAR(50))

RETURNS TinyInt
AS

BEGIN
	IF @sTableName LIKE '#%'
		SET @sTableName = 'tempdb..' + @sTableName
 
	RETURN CASE WHEN object_id(@sTableName) IS NOT NULL THEN 1 ELSE 0 END

END

GO
--test
create table #temp (ch char(1))


--if object_Id(@tablename) is not null
declare @tablename varchar(10)
declare @execS nvarchar(max)
set @tablename = '#temp'

--select @Ret =  dbo.fnTableExists(@tablename) 
if dbo.fnTableExists(@tablename) = 1
  begin
  print 'Trying to delete...'
  set @execS = 'drop table ' + @tablename
  exec sp_executesql @execS
  -- more tests
 end  
GO
create table #temp (ch char(1))
drop table #temp
If it's not broken, fix it until it is.


My Blog
Previous
Reply
Map
View

Click here to load this message in the networking platform