It will work if you put all the if code in a exec statement.
exec('if exists (select * from ' + @gBdTmp + '..sysobjects where id =
object_id(' + @gTableName + ')
exec (drop table ' + @gTableName + ')
else
something else')
If you have a lot of code in the if, it will be hard to maintain but it will work.