Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8725439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:02:29+00:00 2026-06-13T08:02:29+00:00

So I am using the tsql code to run through a bunch of servers,

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T08:02:30+00:00Added an answer on June 13, 2026 at 8:02 am

    Thank you for leading me into the right direction though, instad of

    name<>'APPSWATCH'
    

    I used

    name NOT IN 'APPSWATCH'
    

    and it works

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using SQL Server 2008 R2 and have run into an issue with
Is it possible to get data from remote server using JUST TSQL? I mean
I'm using Visual Studio 2005 to script out a database so that I can
I'm using C# with TSQL and SQL Server 2005 I'm trying to use PetaPoco
I've got the following T-SQL code below that's working from inside of SQL Server
First time using this database because I need a type that's portable and so
I am using Sybase DB with TSQL. The follow snippet of TSQL code is
look at this TSQL code: SELECT A, B, C, (MAX(A) - MAX(B))*C AS 'myCalculatedColumn',
I have ' inherited ' a brilliant piece of TSQL code that does this:
I'm using the following code to restore a backup sql database with C# and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.