+3  A: 

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
Shannon Severance
+2  A: 

You would need to use EXEC to do this.

On the line that says DROP TABLE, you would do:

EXEC('DROP TABLE ' + @TableName)

BUT, I strongly recommend not doing this. Anyone could put any table name (or any other valid SQL in @TableName (called a SQL injection) and cause you all kinds of problems.

I'd read up on some database theory before getting too deeply into trying to learn T-SQL syntax.

SQL Injection Information:
http://unixwiz.net/techtips/sql-injection.html
http://en.wikipedia.org/wiki/SQL_injection
http://msdn.microsoft.com/en-us/library/ms161953.aspx

Michael Todd
Only if he calls the proc with user input--which is definitely not advisable.
Ben M
let's say you have to drop 10 tables. do I need to copy and paste the drop logic 10 times? Is dropping 10 tables a scenario that just never happens in real situations?
MedicineMan
It's pretty rare that you'd be dropping tables as part of your business logic. Tables tend to stick around for a long time.
Ben M
But, as Ben showed you, it'd be easy to write a stored procedure and call it as many times as you need.
Michael Todd