我在一台SQL服务器和一个实例上有多个数据库,而不是3rdparty软件提供的某些过程,所以我无法更改它们。 现在我要从维护计划(脚本)运行这些过程 但是我无法使用命令“ use”更改活动数据库
以下是skript的示例(可在一个DB上运行):
use DB_1 exec spDistinctMask @opMode = 0x08 go use DB_1 exec spDeleteBinEntries @olderThanXDays = 14 go
现在我需要为所有数据库运行此脚本,所以我尝试这样做:
DECLARE CUR CURSOR FOR SELECT name FROM master.dbo.sysdatabases where name not like 'master' and name not like 'tempdb' and name not like 'model' and name not like 'msdb' OPEN CUR PRINT ('******script****** DB: ') FETCH NEXT FROM CUR INTO @NAME WHILE @@FETCH_STATUS = 0 BEGIN SET @Query = CONCAT('USE ', @NAME); EXECUTE (@Query); exec spDistinctMask @opMode = 0x08 PRINT DB_NAME() EXECUTE (@Query); exec spDeleteBinEntries @olderThanXDays = 7 FETCH NEXT FROM CUR INTO @NAME END PRINT ('end of script******') CLOSE CUR DEALLOCATE CUR
但是似乎选择的DB是DB_1,即使在服务器上是DB_2,也是DB_3 如何在多个数据库中执行脚本?