There is a naive but surprisingly effective way to solve the ordering problem: keep running each script. An individual script will either work (after running other scripts) or crash> scripts # (in this case, this is a bad script). You might find a bad script faster / easier, but I never needed it.
If you have 1 gigantic script, it will probably be shared by GO statements. This should be enough to run it as many times as there are unique GO statements. Any attempts to create an object that already exists will fail and interrupt the package. The next batch will run smoothly. In the end, you have the necessary objects created - and re-playing the entire script will create a dependent object (and the already created independent objects will crash). However, you will never get a script to run without errors.
If you want to get a little involved, you can split the giant script into separate lots and run them individually. Now you can track what order is needed to make them work. Just recompile them in that order and output a new script. It should work without errors.
Or spend $ 500 on a tool that already does this (RedGate, Visual Studio Ultimate / Database Edition, etc.).
source share