我想程序应该是这样的:
declare @db varchar(100)
declare @user varchar(100)
declare c cursor for select name from sys.sysdatabases
open c
fetch next from c into @db
while @@fetch_status = 0
begin
print @db
exec ('use ' + @db)
declare u cursor for select name from sys.sysusers
where issqlrole <> 1 and hasdbaccess <> 0 and isntname <> 1
open u
fetch next from u into @user
while @@fetch_status = 0
begin
print @user
fetch next from u into @user
end
print '--------------------------------------------------'
close u
deallocate u
fetch next from c into @db
end
close c
deallocate c
但问题是 exec('use' + @db) 不起作用。我总是得到当前选择的数据库的用户列表。我应该如何解决这个问题?
P.S.:我希望这段代码可以在 2000 和 2005 sql 服务器上运行。
您还可以使用未记录但经常使用的sp_MSforeachdb
存储过程 - 请参阅此处了解详细信息 http://www.databasejournal.com/features/mssql/article.php/3441031/SQL-Server-Undocumented-Stored-Procedures-spMSforeachtable-and-spMSforeachdb.htm或看另一个博客文章在这里 http://www.kodyaz.com/articles/sql-server-exec-sp_msforeachdb-sp_msforeachtable-example-tsql-code.aspx:
exec sp_MSforeachdb 'select * from ?.sys.sysusers'
这 ”?”是将添加到命令中的数据库名称的占位符,因为它针对系统中的每个数据库执行。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)