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

  • Home
  • SEARCH
  • 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 3489642
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:25:10+00:00 2026-05-18T11:25:10+00:00

I’m using SQL Server 2005 and am logged in as sa . I would

  • 0

I’m using SQL Server 2005 and am logged in as sa. I would like to query every table in my database for whether it has a certain column name. And if so append every row where (columnNameValue = someValue) to a results table. Then return said results table.

There are a few similar problems with solutions out there. Notably I can use sp_MSForeachTable for this but it lacks any kind of documentation. I can use SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES to get a list of all the tables.

The following solution (link text) will go every table & every column to find a certain value in the cell. Which is different from finding a certain value in the cell IF that column is a certain columnName.

Writing a nested while statement should be possible but are there any inbuilt commands to run queries like this?

Pseudo-Code if it helps :

foreach(table in tableList) {
    if (table.hasColumnName(SOME_COLUMN) {
       EXEC ('SELECT * FROM table WHERE (SOME_COLUMN = SOME_VALUE)')
    }
}

[Edit]

Rather then having a single results set I would like to be a single result per table as long as the select returns at least one row. This should give a very large amount of different results as expecting a join or union to work is unrealistic.

If possible I would like to append the tablename to start of each result.

I have a simple query below that gets all the results but It will display empty tables and doesnt give any visual indication in individual results as to what table it belongs to:

[Further Edit]

Updated the query below to include the IF EXIST check that removes NULL results & the select name AS source column to add the table source to the results thanks to @Martin

DECLARE @COLUMN_VALUE nvarchar(512), @VALUE nvarchar(10);

SET @COLUMN_VALUE = 'id'
SET @VALUE = '0';

DECLARE @TABLE_NAME nvarchar(512), @COLUMN_NAME nvarchar(512), @QUERY nvarchar(512);

SET @TABLE_NAME = '';

WHILE @TABLE_NAME IS NOT NULL
BEGIN
    SET @TABLE_NAME =
    (
        SELECT MIN(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME))
        FROM INFORMATION_SCHEMA.TABLES
        WHERE QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) > @TABLE_NAME
    );
    PRINT 'Table name : ' + @TABLE_NAME;
    SET @COLUMN_NAME = 
    (
        SELECT MIN(QUOTENAME(COLUMN_NAME))
        FROM INFORMATION_SCHEMA.COLUMNS
        WHERE (TABLE_NAME = PARSENAME(@TABLE_NAME, 1))
            AND (COLUMN_NAME = @COLUMN_VALUE)

    );
    PRINT 'Column name : ' + @COLUMN_NAME;
    IF @COLUMN_NAME IS NOT NULL 
    BEGIN
        SET @QUERY = 
            'SELECT ''' + @TABLE_NAME + ''' AS Source, * ' +
            'FROM ' + @TABLE_NAME + ' ' +
            'WHERE (' + @COLUMN_NAME + ' = ' + @VALUE + ')'
        EXEC
        (
            'IF EXISTS(' + @QUERY + ') ' + @QUERY
        )
    END     
END
  • 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-05-18T11:25:10+00:00Added an answer on May 18, 2026 at 11:25 am
    CREATE TABLE foo
    (
    SOME_COLUMN VARCHAR(10)
    )
    
    CREATE TABLE bar
    (
    SOME_COLUMN VARCHAR(10)
    )
    
    INSERT INTO bar VALUES ('SOME_VALUE')
    
    
    DECLARE @Query nvarchar(max)
    
    SELECT 
          @Query = isnull(@Query + ';','') + 
          'IF EXISTS(SELECT * FROM ' + QUOTENAME(SCHEMA_NAME(schema_id)) + '.' + 
          QUOTENAME(o.name) + ' WHERE SOME_COLUMN=''SOME_VALUE'')
          SELECT ''' + o.name +''' AS Source, * FROM ' + QUOTENAME(SCHEMA_NAME(schema_id)) + '.' + 
          QUOTENAME(o.name) + ' WHERE SOME_COLUMN=''SOME_VALUE'''
    FROM sys.columns c 
    JOIN sys.objects o
    ON o.object_id = c.object_id
    WHERE o.type IN ('U','V') AND c.name = 'SOME_COLUMN'
    
    EXEC sp_executesql @Query
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.