How to find all SQL tables with a column name

Often I need to find all the tables in Dynamics GP that have a particular column or search for all columns where I only have a partial column name. Over the years I have seen lots of different code to accomplish this, so this is nothing new. But I am asked about this enough that I thought I would share the code I use:

SELECT
    TABLE_NAME, COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME in
   (SELECT name
    FROM sysobjects
    WHERE xtype = 'U')
and COLUMN_NAME like '%xxxx%' -- replace with yours
ORDER BY COLUMN_NAME, TABLE_NAME

Just replace the xxxx with what you are looking for and run.

Updated on Jan 19, 2011 to add character maximum length.

4 Responses to “How to find all SQL tables with a column name”

  1. Hi Victoria,

    I used this query to find out POP and PM link, which is not available on Microsoft site. So I used this query. Following is the modified script.

    SELECT
    TABLE_NAME, COLUMN_NAME, DATA_TYPE
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_NAME in
    (SELECT name
    FROM sysobjects
    WHERE xtype = ‘U’)
    and COLUMN_NAME like ‘VCHRNMBR’ — replace with yours
    ORDER BY COLUMN_NAME, TABLE_NAME

    and my required result is POP30300.

    Thanks for your help.

  2. Hi Victoria,

    Very helpful post.

    Thanks

    Waseem.

  3. Hi Victoria,

    Thanks for this new post. It is good and knowledable. But what is the meaning of xtype = U?

    Thanks

Leave a Reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

Please log in to WordPress.com to post a comment to your blog.

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 405 other followers