I am using a relational tables. On a button click i have to retrieve the all the tables in my database in such a way that all the Master tables are coming at the Top.( i.e Generate Scripts from the frontend) and save them onto a text file.
Back Ground purpose: I need to generate script for all the tables in the order and while executing the generated script Foreign key Constraint error will not occur.So I need to get table in the order of relations.
How can I retrieve the table in that way?
If you use SQL Server Management Studio:
Right click [Your DB] -> Tasks -> Generate Scripts -> Next -> Next -> [Select Tables] Next -> … all the way to the end,
the wizard should be able to create an intelligent scripts that creates dependent objects first.
EDITED (Oct 18, 2011):
If you generate your scripts from the output of the following query, you should have to worry about foreign key dependencies giving you problems: