I have to create a full copy of a remote db. I have only one useful and easy way to do it: by generating scripts. I am aware about other ways (create backup, for instance) but I can't use them for some reason.
Since one table might be depend on another (has a foreign key, etc...) and because of this it's pretty difficult to figure out the valid order of creating tables, I decide to generate these "dependable" script instructions at the very end.
Well, I know how to generate that "dependable" script instructions within (close to) table creation. But what should I do to skip them first only to generate them at the end?
For instance:
--How I do NOT want
--.........................
CREATE TABLE Table1(....)
ALTER TABLE Table1 ADD CONSTRAINT ...
CREATE TABLE Table2(....)
ALTER TABLE Table2 ADD CONSTRAINT ...
--How I would like
--.........................
CREATE TABLE Table1(....)
CREATE TABLE Table2(....)
--.........................
CREATE TABLE TableN(....)
--.........................
--the end of a script
ALTER TABLE Table1 ADD CONSTRAINT .....
Presumably, you have code that already loops through the tables and calls the Script()
method on each table. What you're going to want to do is create a ScriptingOptions object, set its DriForeignKeys property to false, and call the Script()
method with that ScriptingOptions object as a parameter. Then, once you've looped through all of the tables, you're going to need to do it again, this time to pick up the foreign keys.