Jitendra Kumar

Jitendra Kumar

  • 680
  • 2.5k
  • 720.7k

Query that returns all tables order by Primary foreign const

Apr 21 2016 3:23 AM
Here is the simple SQL query that will return all tables from the database order by primary foreign constraints.
SELECT t.TABLE_NAME FROM INFORMATION_SCHEMA.TABLES t
Left Join INFORMATION_SCHEMA.CONSTRAINT_TABLE_USAGE cu on t.TABLE_NAME=cu.TABLE_NAME WHERE t.TABLE_TYPE = 'BASE TABLE'
Order by cu.CONSTRAINT_NAME desc
or
Select * from sys.tables t
Inner join sys.objects o on o.object_id=t.object_id
I perform insert statement these so that I want tables in primary foreign order.
otherwise, I get the error like this
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_emp1". The conflict occurred in database "testdata", table "dbo.Emp".
However, what I would like to do here is to get all the tables order by primary foreign constraints. How should I query this?
Thank you very much.

Brought to you by:

Answers (1)