How to generate sql scripts using SMO scripter

青春壹個敷衍的年華 提交于 2019-12-04 03:54:08

You need to discover and sort the tables in dependency order in your PowerShell script before scripting out the objects. Review an implementation of this on the following blog: http://patlau.blogspot.com/2012/09/generate-sqlserver-scripts-with.html

The concept was clearer to me in C#. Check out: http://sqlblog.com/blogs/ben_miller/archive/2007/10/18/scripting-tables-views-and-data-using-smo-part-3.aspx

muhmud
  1. Script all tables without foreign keys, then script out just the foreign keys. You can do this by using the scripting options, and this way the dependencies between tables due to FKs won't matter.
  2. Use the DependencyWalker SMO object. You can add the URNs for the views to it and then ask for a linear list of dependencies. My answer here to a similar question contains an example of how to use it.

You could take a different approach and script out tables and then views. Generally in that order, things work, but not 100%.

I would set WithDependencies to false

It's admittedly a hack, but you could patch the script before you run it by adding error handling. You could probably leverage the new parser in V3 to automate that.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!