You can't drop a table with a function: you have to do it in a stored proc. (Functions cannot modify the database.) Something like:
CREATE PROCEDURE SAFE_DROP_TABLE(@TableName nvarchar(30))
AS
BEGIN
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(@TableName)
AND TYPE IN (N'U'))
EXEC ('DROP TABLE ' + @TableName) -- note change here
IF @@ERROR <> 0
BEGIN
PRINT 'Failed to drop ' + @TableName + ' Error = ' + @@ERROR
END ELSE BEGIN
PRINT 'dropped' + @TableName
END
ELSE
PRINT 'NO ACTION NEEDED'
END
END
Ben M
2009-07-24 22:59:31