sql - How do I write a TSQL function that takes a table name to drop, and drops a table -
Sorry, but I'm new to TSQL, so I do not even know the basics. I am working on this function, but I am running in several syntax issues. If someone can help me write this function, then I appreciate it.
1 Before trying to leave the table, I would like to check the existence of the table. After doing 2 plus, I want to check the errors. 3 I get this status message I would like to print which includes the name of the table - 4 I realize that I do not need to print 'no action', but I want to make it work a bit more challenging in any way I am including.
5 I have a lot of syntax error issues that I do not know how to clean
Create function is started as SAFE_DROP_TABLE (@TableName NVARCHAR (30)) (from sys Select * to run in. Objects WHERE object_id = OBJECT_ID (@TableName) AND TYPE IN (N'U ') drop table @TableName IF @@ error & lt; & Gt; 0 'Print failed to drop' + @TableName + 'error =' + @@ Print error from left '' left '+ @TableName END ELSE print' no action required 'End of End
You have a table with a function You can not leave it: You have to do this in a stored proc (can not modify the working database.) Create something like:
create process SAFE_DROP_TABLE (@TableName nvarchar (30)) Start in form exists (select * from sys.objects where object_id = object_id (@TableName) and (N'U type ')) EXEC (' Drop Table '+ @TableName) - Tips Lead here change @@ error & lt; & Gt; 0 start print + @TableName + 'failed to drop' error = '+ @@ error end and print of BEGIN + @TableName end and print' no action required 'END END
< / 'Dropped' div>