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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:22:22+00:00 2026-05-16T21:22:22+00:00

I’m trying to print out Tables from the DB that have the EntityId column

  • 0

I’m trying to print out Tables from the DB that have the EntityId column equals to DataclassId column here is the code

    public void getRootTables_checkSP()
    {
        string connect = "Data Source= EUADEVS06\\SS2008;Initial Catalog=TacOps_4_0_0_4_test;integrated security=SSPI; persist security info=False;Trusted_Connection=Yes";
        SqlDataReader rootTables_List = null;
        SqlConnection conn = new SqlConnection(connect);
        conn.Open();

        SqlCommand s_cmd = new SqlCommand("SELECT * FROM sys.Tables WHERE EntityId = DataclassId", conn);
        rootTables_List = s_cmd.ExecuteReader();

        while (rootTables_List.Read())
        {
            string test = rootTables_List[0].ToString();
            Console.WriteLine("ROOT TABLES ARE {0}", test);
        }
        rootTables_List.Close();
        conn.Close();
    }

but it keeps saying that these columns are invalid though when I printed out all the columns in the DB “syscolumns” they were there…

Can anyone tell me why I’m getting such an error?

EDIT

What I really want is to query the db TacOps_4_0_0_4_test not the system. I just realized that

EDIT 2

Here is an example of the Tables in my DB

Table_1
ID  Sequence    Type    Heigh  Weight   EntityId    DataclassId
0   1           s       1.4     2.5     42-2c-Qi    42-2c-Qi
1   2           s       2.4     2.5     zh-km-xd    zh-km-xd
2   3           s       3.4     2.5     8n-tr-l7    8n-tr-l7

Table_2
ID  Data    Person    EntityId    DataclassId
0   1        Dave     58-zj-4o    41-2c-Q7
1   2        Sara     99-op-t6    oy-7j-mf
2   3        Silve    75-qy-47    2d-74-ds

Table_3
ID  Name    Genre   EntityId    DataclassId
0   LR      Ac      78-jd-o9    78-jd-o9
1   OI      Dr      4t-jb-qj    4t-jb-qj
2   DH      Do      7j-3e-ol    7j-3e-ol

The output should be

Table_1
Table_3
  • 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-16T21:22:22+00:00Added an answer on May 16, 2026 at 9:22 pm

    I think I may understand what you’re trying now based on your comment to Frederik’s answer

    I tried “syscolumns” just to make sure
    that the columns do exist. But when I
    do the query where EntityId =
    DataclassId it says “Invalid column
    name

    It sounds like EntityId and Dataclassid are columns in a table (or tables) that you have in your database and you want to find the rows from those tables that contain the same value in both those columns??

    If that’s the case, you are querying sys.Tables incorrectly – you’d need to query the specific tables directly i.e.

    SELECT * FROM Table1 WHERE EntityId = DataClassId
    

    Can you clarify?

    Edit:
    You can find all the tables that contain both those columns using this:

    SELECT t.name
    FROM sys.tables t
    WHERE EXISTS(SELECT * FROM sys.columns c WHERE c.object_id = t.object_id AND c.name='EntityId')
        AND EXISTS(SELECT * FROM sys.columns c WHERE c.object_id = t.object_id AND c.name='DataClassId')
    

    From this, you could either iterate round each table and run the query to find rows that match on EntityId/DataClassId values – could insert into a temp table and return 1 resultset at the end.

    OR, you could create a view to UNION all the tables together and then query that view (would need to update the view each time you added a new table).

    OR, you could do some dynamic SQL generation based on the above to generate a SELECT statement on-the-fly to UNION all the tables together.

    Update:
    Here’s a generic way to do it in pure TSQL – this way means if new tables are added it will automatically include them:

    DECLARE @SQL VARCHAR(MAX)
    
    SELECT @SQL = COALESCE(@SQL + CHAR(10) + 'UNION ALL' + CHAR(10), '') 
        + 'SELECT ''' + REPLACE(QUOTENAME(t.Name), '''', '''''') + ''' AS TableName, COUNT(*) AS RowsMatched FROM ' + QUOTENAME(t.name) 
        + ' WHERE EntityId = DataClassId'
    FROM sys.tables t
    WHERE EXISTS(SELECT * FROM sys.columns c WHERE c.object_id = t.object_id AND c.name='EntityId')
        AND EXISTS(SELECT * FROM sys.columns c WHERE c.object_id = t.object_id AND c.name='DataClassId')
    
    SET @SQL = 'SELECT x.TableName, x.RowsMatched FROM (' + @SQL + ') x WHERE x.RowsMatched > 0 ORDER BY x.TableName'
    EXECUTE(@SQL)
    

    If you don’t need it to be dynamic, change the above EXECUTE to a PRINT to see the SQL it generates, and then create a view from it. You can then SELECT from that view.

    Of course, you could either loop round each table 1 by 1 as you are trying.

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
I have a bunch of posts stored in text files formatted in yaml/textile (from

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.