I need to generate drop and create scripts for all the keys, constraints in the SQL server database. This exercise is to generate upgrade scripts for the database in the target environment . I have the script, but its failing because I need to find the right order of the database objects like, parent, child table level in order to crate them. For example, to crate/drop indexes , basically i capture the indexes using the below query
'SELECT object_name(si.object_id)
,si.object_id
,si.NAME
,si.index_id
FROM sys.indexes si
LEFT JOIN information_schema.table_constraints tc ON si.NAME = tc.constraint_name AND object_name(si.object_id) = tc.table_name
WHERE objectproperty(si.object_id, 'IsUserTable') = 1
ORDER BY object_name(si.object_id)
,si.index_id'
Let us know how do we include order /level of indexes. Similarly need to make sure primary,foreign,constraints while generating scripts.
Can you let me know what is the right way to capture this.