.net - Query to retrieve the name of an specific table in database and retrive primary key of that table -


I have a set of tables with "location", such as location bookings and location around their name. I want to write a quesry first to get back those table names from my table and then get my primary key. How do I do that? [A_rahnema]. [Dbo] is the name of my database.

This query will give you a comprehensive table that includes the primary name's name, table name, primary key name and column For tables containing "Places" for all parts of their name: Select

  from INFORMATION_SCHEMA. TABLE_CONSTRAINTS TC Internal included INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu at tc.CONSTRAINT_CATALOG = kcu.CONSTRAINT_CATALOG and tc.CONSTRAINT_SCHEMA = kcu.CONSTRAINT_SCHEMA and tc.CONSTRAINT_NAME = kcu.CONSTRAINT_NAME where tc.CONSTRAINT_TYPE = 'primary key' and 'TCC.' Like 'TABLE_NAME', the '% location%' command Tc.CONSTRAINT_CATALOG, tc.CONSTRAINT_SCHEMA, tc.CONSTRAINT_NAME, kcu.ORDINAL_POSITION   

You can further limit the columns for your actual query.


When writing SQL, you are encouraged to think as a whole set of results, and in terms of telling the engine what do you want , No how to do it . So you should not be thinking as "I do this first, then I do this next thing, then for every result, I want to do something else".

Comments