title: Script all views/functions/procedures in a Sql Server database in dependency order
body: <p>Sql Server 2008 (and probably most other versions): Management Studio has a 'generate scripts' option that can in theory script a whole database with all objects (Right click, tasks, Generate Scripts). It works OK for most things but when you use it to script all views/stored procedures/functions in a database, it generates a script that does not take account of dependencies between the objects.</p>
  
  <p>e.g. If View A references Function B it won't necessarily put Function B in the script first.</p>
  
  <p>It takes a long time to untangle the great long script that gets produced so that it is in an order that will run without errors.</p>
  
  <p>There must be a better way. Whats the best way to get round this, preferably without spending any money?* </p>
  
  <p>* (red gate ftw)</p>