您不能在DDL中使用变量,例如@tableName。此外,将名称分成部分并忽略架构只会导致错误。您应该只在SQL批处理参数中使用“?”替换,然后依靠
MSforeachtable替换:
EXEC sp_MSforeachtable 'if not exists (select * from sys.columns where object_id = object_id(''?'') and name = ''CreatedOn'') begin ALTER TABLE ? ADD CreatedOn datetime NOT NULL DEFAULT getdate();end';


