按依赖顺序编写 Sql Server 数据库中所有视图/函数/过程的脚本
Sql Server 2008(可能还有大多数其他版本):Management Studio 有一个“生成脚本”选项,理论上可以为包含所有对象的整个数据库编写脚本(右键单击、任务、生成脚本)。它适用于大多数情况,但是当您使用它为数据库中的所有视图/存储过程/函数编写脚本时,它会生成一个不考虑对象之间依赖关系的脚本。
例如,如果视图 A 引用函数 B,则不一定首先将函数 B 放入脚本中。
需要很长时间才能理清生成的长脚本,使其按顺序运行而不会出现错误。
一定有更好的方法。解决这个问题的最佳方法是什么,最好是不花任何钱?*
* (red gateway ftw)
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.
e.g. If View A references Function B it won't necessarily put Function B in the script first.
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.
There must be a better way. Whats the best way to get round this, preferably without spending any money?*
* (red gate ftw)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不幸的是,创建此类脚本的唯一快速简便的方法是使用一些第三方工具。我们使用 Apex Script 但还有其他工具,Red Gate 可能有自己的工具这个版本。
其他选项包括:
之前的 sys 依赖关系视图中有一个错误SQL Server 版本。我记得在 SQL 2008 即将发布时读到过它。
我不记得所有细节,但这是关于当对象被删除并重新创建时依赖项无法正常工作的问题。
Unfortunately, the only quick and easy way to create such a script is using some third party tools. We’re using Apex Script but there are also other tools out there and Red Gate probably has its own version of this.
Other options are:
There was a bug in sys dependencies views in previous versions SQL Server. I remember reading about it when SQL 2008 was about to be released.
I don’t remember all the detail but it was something about dependencies not working correctly when objects were dropped and re-created.
这是一个穷人的方法:
sys.sql_dependency 列出自
自下而上。即列表基数
首先是物体,然后是其上的物体
这些取决于哪些,等等。这将
给你顺序
为您的对象编写脚本。
对象出来。
Here's a poor-man's approach:
sys.sql_dependencies that lists from
the bottom up. That is, list base
objects first, then the objects upon
which those depend, etc. This will
give you the order in which to
script your objects.
objects out.