I'm no SQL expert so correct me if I'm wrong, but...
This script gives me syntax errors because some of the table names in my database include spaces, hyphens and other strange characters.
So shouldn't we put brackets around the table names, i.e.
if exists (select * from sysreferences where rkeyid = object_id(@tn))
/*table referenced by a FOREIGN KEY constraint */
set @sql = 'delete [' + @TN + ']'
set @sql = 'truncate table [' + @TN + ']'
Before someone says "You shouldn't put such characters in table names", the table names are not under my control.