script to determine which database tables include both columns.

  • Hi ,

    script to determine which database tables include both columns. Any suggestions please.

  • There are probably lots of ways of doing this but the following should work.  It counts how many of the columns exist in each table and if it's more than 1 then they both exist:

     select object_name(object_id),
    count(*)
    from sys.columns
    where name in ('ColumnName1', 'ColumnName2')
    group by object_name(object_id)
    having count(*)>1

    building on this you can add a case to show the values in your example:

     select object_name(object_id), 
    case when count(*)>1 then 'Exist' else 'Not Exist' end ColsExist
    from sys.columns
    where name in ('ColumnName1', 'ColumnName2')
    group by object_name(object_id)

    • This reply was modified 2 years, 2 months ago by  DNA_DBA.

Viewing 2 posts - 1 through 1 (of 1 total)

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