So I am using the tsql code to run through a bunch of servers, and look in each database for the users. My issue that on a specific server, there are databases I do not have access to and do not need to use. When the query runs on them it stops on the entire server and moves to the next one. I have been trying to find a way to exclude certain databses from the search.
What I am trying to do is
for example on server A excluded these databses B,C,D, and so on . I have tried where <> and != and does not work or I have the wrong syntax
USE MASTER
If OBJECT_ID('#TDB', 'U') > 0
Drop Table #TDB
DECLARE @dbname varchar(200),
@sql varchar(max)
CREATE TABLE #TDB (
DataBaseName nvarchar(200),
UserName nvarchar(200)
)
DECLARE db_cursor CURSOR FOR
SELECT name
FROM master.dbo.sysdatabases WHERE DBID>4
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @dbname
WHILE @@FETCH_STATUS = 0
BEGIN
SET @sql='insert into #TDb(DataBaseName,UserName)
select '''+@dbname+''' DataBaseName,[user_name] UserName FROM '+@dbname+'.[dbo].[USERS] where'+@dbname+'<>[APSSWATCH]'
EXEC(@sql)
FETCH NEXT FROM db_cursor INTO @dbname
END
CLOSE db_cursor
DEALLOCATE db_cursor
SELECT * FROM #TDB ORDER BY DataBaseName,UserName
DROP TABLE #TDB
Thank you for leading me into the right direction though, instad of
I used
and it works