更新时间:2022-05-17 22:19:03
您需要将所有命令放在一个动态 SQL 中并运行它,因为当第一个包含 USE 命令的动态 SQL 被执行时,USE DATABASE 将退出.
You need to put all the command in one dynamic SQL and run it because the USE DATABASE will be out of when the first dynamic SQL containing the USE command is executed.
因此,删除除最后一个之外的所有 EXEC,并连接@sqlcmd 中的所有 SQL.运行 EXEC @sqlcmd.
So, remove all the EXEC except the last one, and concat all the SQL in @sqlcmd. Run EXEC the @sqlcmd.