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 8080425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:24:34+00:00 2026-06-05T16:24:34+00:00

I am summer intern new to T-SQL and I have to run an sql

  • 0

I am summer intern new to T-SQL and I have to run an sql select statement on various databases. What I would like to do is use ‘if exists’ to keep an error from occuring because some of the databases on the list to have this statement executed on no longer exist. However, I cannot figure out how to apply it to my statement. Any help would be greatly appreciated. Below is the statment me and another intern wrote:

select distinct mg.MatterName, mg.ClientNumber, mg.MatterNumber,grp.groupName as SecurityGroup
from (select distinct mat.matterName, mat.clientNumber, mat.matterNumber, usr.GroupID
    from <db_name>.dbo.matter mat
    inner join <db_name>.dbo.usrAccount usr
    on usr.NTlogin=mat.matterCreateBy) as mg
          inner join <db_name>.dbo.usrGroup grp
          on mg.groupID=grp.groupID
order by matterName

the < db_name> is where the passed in parameter that is the name of the database, would go.

  • 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-05T16:24:37+00:00Added an answer on June 5, 2026 at 4:24 pm

    You could use sp_MSforeachdb to enumerate all of the databases on the instance.

    This would be similar to:

    exec sp_MSforeachdb 'select distinct mg.MatterName, mg.ClientNumber, mg.MatterNumber,grp.groupName as SecurityGroup from (select distinct mat.matterName, mat.clientNumber, mat.matterNumber, usr.GroupID     from ?.dbo.matter mat     inner join ?.dbo.usrAccount usr     on usr.NTlogin=mat.matterCreateBy) as mg           inner join ?.dbo.usrGroup grp           on mg.groupID=grp.groupID order by matterName'
    

    Alternatively, you could use dynamic sql to manufacture a script:

    select 'use ' + name + ';' + char(13) + 'select distinct mg.MatterName, mg.ClientNumber, mg.MatterNumber,grp.groupName as SecurityGroup' +CHAR(13) + 'from (select distinct mat.matterName, mat.clientNumber, mat.matterNumber, usr.GroupID' + char(13) + 'from dbo.matter mat' + char(13) + 'inner join dbo.usrAccount usr on usr.NTlogin=mat.matterCreateBy) as mg' + char(13) + 'inner join dbo.usrGroup grp on mg.groupID=grp.groupID' + CHAR(13) + 'order by matterName;'
    from master.sys.databases where database_id>4
    

    If you redirect your output to “Results to Text” in SSMS then run the script, you will see a script written that you can then put into a query editor to execute.

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

Sidebar

Related Questions

As my project to do over this summer I would like to create a
I would like to start a Summer Project just for the sake of improving
I have been planning since Summer to build a new hobby site over Christmas
My first post here :) I have a summer job doing a bit of
As a project over summer while I have some downtime from Uni I am
I've been learning C# over the summer and now feel like making a small
I have a project that is coming around the bend this summer that is
I am trying to have some fun in summer. Writing a piece of code
Last summer, I made a Java application that would parse some PDF files and
I have plans this summer to build an E-commerce app for digital downloads 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.