Dynamically determining table name given field name in SQL server

Posted by Salman A on Stack Overflow See other posts from Stack Overflow or by Salman A
Published on 2010-05-07T14:35:14Z Indexed on 2010/05/07 14:38 UTC
Read the original article Hit count: 286

Filed under:
|
|

Strange situation: I am trying to remove some hard coding from my code. There is a situation where I have a field, lets say "CityID", and using this information, I want to find out which table contains a primary key called CityID.

Logically, you'd say that it's probably a table called "City" but it's not... that table is called "Cities". There are some other inconsistencies in database naming hence I can never be sure if removing the string "ID" and finding out the plural will be sufficient.

Note: Once I figure out that CityID refers to a table called Cities, I will perform a join to replace CityID with city name on the fly. I will appreciate if someonw can also tell me how to find out the first varchar field in a table given its name.

© Stack Overflow or respective owner

Related posts about sql

Related posts about sql-server