returning tables that contain a specific field

  • Hi,

    Is there any way that I can query all my user tables to find in which table a certain fieldName exists?

    Eg, I have several tables that contain the field “storeId”, is there any way that I can return the names of all my user tables that contain the field “storeId” ?

    Eg

    SELECT userTableName

    FROM userTablesCollection

    Where fieldname = “storeId”

    I ask this because it would save time from manually searching thru all my user tables.

    Any help greatly appreciated.

    Cheers,

    Yogi.

  • Hi yogi,

    quote:


    Hi,

    Is there any way that I can query all my user tables to find in which table a certain fieldName exists?


    several ways to do this

    SELECT

    table_schema, table_name

    FROM

    information_schema.columns

    WHERE

    column_name = 'MsgDateReceived'

    OR

    SELECT

    s2.name

    FROM

    dbo.syscolumns s1

    JOIN

    dbo.sysobjects s2

    ON

    s1.id = s2.id

    WHERE

    s1.NAME='MsgDateReceived'

    Or (according to Execution Plan the most efficient one)

    SELECT

    table_schema, table_name

    FROM

    information_schema.columns

    WHERE

    column_name

    like 'MsgDateReceived'

    AND

    objectproperty(object_id(table_name), 'IsUserTable') = 1

    ORDER BY

    column_name

    HTH

    Frank

    http://www.insidesql.de

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • Morning Frank,

    This has opened up quite a few avenues for me, thanks man.

    btw are you a stuttgart or a bayern fan?

    yogiberr(glasgow)

  • quote:


    btw are you a stuttgart or a bayern fan?


    neither. I am (originally) from Gelsenkirchen. While I am not much interested in soccer and nobody knows Gelsenkirchen, soccer fan will maybe know Schalke 04?

    Frank

    http://www.insidesql.de

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • quote:


    Hi,

    Is there any way that I can query all my user tables to find in which table a certain fieldName exists?

    Eg, I have several tables that contain the field “storeId”, is there any way that I can return the names of all my user tables that contain the field “storeId” ?

    Eg

    SELECT userTableName

    FROM userTablesCollection

    Where fieldname = “storeId”

    I ask this because it would save time from manually searching thru all my user tables.

    Any help greatly appreciated.

    Cheers,

    Yogi.


    SELECT Table_Name

    FROM Information_Schema.Columns

    WHERE Column_Name = 'StoreID'

    --Jonathan



    --Jonathan

  • thanks Jonathan.

    yogi

  • hi yogiberr!

    the thing you're talking about is named "database dictionary" (take a look at BOL). for a glance of what information is "hidden" there, look at

    http://www.sqlservercentral.com/scripts/contributions/246.asp

    regards,

    chris.

  • thanks Chris.

    looks like "database dictionary" will save me a good bit of time.

    Magic.

    yogiberr

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply