我正在尝试通过我在表中列出的多个数据库循环查询,我尝试使用游标并使用新的数据库名称切换 USE 语句,但我不断收到不正确的语法错误。有没有办法做到这一点?下面是我的尝试。
Declare @storednamesDATABASE varchar(255)
Declare storednamesDBs_cursor Cursor
for
select Database_name from ##ALLTHEstorednames
Open storednamesDBs_cursor
Fetch next from storednamesDBs_cursor into @storednamesDATABASE
WHILE @@FETCH_STATUS = 0 BEGIN
Use @storednamesDATABASE
select distinct a.database_id, c.name as Database_Name, a.object_id, b.name as Table_Name,
dateadd(day, datediff(day, 0, getdate()),0) as Use_Date
from sys.dm_db_index_usage_stats a
join sys.all_objects b
on a.object_id = b.object_id
join sys.databases c
on a.database_id = c.database_id
where (a.last_user_seek between dateadd(hour, -24, CURRENT_TIMESTAMP) AND CURRENT_TIMESTAMP
or a.last_user_scan between dateadd(hour, -24, CURRENT_TIMESTAMP) AND CURRENT_TIMESTAMP
or a.last_user_lookup between dateadd(hour, -24, CURRENT_TIMESTAMP) AND CURRENT_TIMESTAMP)
and b.type = 'u'
End
Close storednamesDBs_cursor