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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:54:57+00:00 2026-06-09T16:54:57+00:00

I have a DataBase with around +100 tables, like half of tables have column

  • 0

I have a DataBase with around +100 tables, like half of tables have column A & column B.

My question is, Can I query all tables that have this columns with a specific values e.g.

SELECT * FROM DATABASE

WHERE

EACHTABLE HAS COLUMN A = 21 //only if table has columns and then values

AND

COLUMN B = 13

I am not sure how exact I will do it, nothing is coming up on google either

  • 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-09T16:54:58+00:00Added an answer on June 9, 2026 at 4:54 pm

    You can use the undocumented MS stored procedure sp_MSforeachtable, if you fancy living life recklessly:

    create table T1 (
        ColumnA int not null,
        ColumnB int not null
    )
    go
    create table T2 (
        ColumnA int not null,
        Column2 int not null
    )
    go
    create table T3 (
        Column1 int not null,
        ColumnB int not null
    )
    go
    create table T4 (
        ColumnA int not null,
        ColumnB int not null
    )
    go
    insert into T1 values (1,2);
    insert into T2 values (3,4);
    insert into T3 values (5,6);
    insert into T4 values (7,8);
    go
    create table #Results (TableName sysname,ColumnA int,ColumnB int)
    exec sp_MSforeachtable 'insert into #Results select ''?'',ColumnA,ColumnB from ?',
        @whereand = ' and syso.object_id in (select object_id from sys.columns where name=''ColumnA'') and syso.object_id in (select object_id from sys.columns where name=''ColumnB'')'
    select * from #Results
    drop table #Results
    

    Result:

    TableName                             ColumnA     ColumnB
    ------------------------------------- ----------- -----------
    [dbo].[T1]                            1           2
    [dbo].[T4]                            7           8
    

    By default, sp_MSforeachtable will, as its name implies, perform the same task for each table in the database. However, one optional parameter to this procedure, called @Whereand, can be used to modify the WHERE clause of the internal query that enumerates the tables in the database. It helps to know that this internal query has already established two aliases to some of the system views. o is an alias for sysobjects (the legacy system view). syso is an alias for sys.all_objects (a more modern system view).

    Once sp_MSforeachtable has decided which tables to run against, it will execute the query given to it as its first parameter. But, it will replace ? with the schema and table name (? is the default replacement character. This can be changed as needed)

    In this case, I chose to create a temp table, then have each selected table store its results into this temp table, and after sp_MSforeachtable has finished running, to select the combined results out with no further processing.

    There is a similar (and similarly undocumented) procedure called sp_MSforeachdb which will access each user database on the server. These can even be combined (although you have to be careful with doubling up ' quote characters twice, at times). However, there’s no equivalent sp_MSforeachcolumn.

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

Sidebar

Related Questions

Consider a database(MSSQL 2005) that consists of 100+ tables which have primary keys defined
So, I have a database with around 100 audit tables and I want to
I have a database that has around 10k records and some of them contain
We have an Oracle database here that's been around for about 10 years. It's
I have a database table, UserRewards that has 30+ million rows. In this row,
We have a database that has more than 70 tables with more than 10k
I have some legacy DB with many tables (around 100) the old system is
i have this table of comments (little over 1 milion rows) that gets around
My database has around 100 tables and anywhere from 1,000 to 20,000 rows per
I have a huge table in my database that contains distances between cities. This

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.