如何以编程方式检索 SQL Server 2005 中视图的更改视图脚本
我们允许我们的用户更改报告的某些视图,以及不基于我们在应用程序中跟踪的某些应用程序字段元数据的内容。这些字段可以在运行时创建。我有一个标准流程来在添加或删除字段时更改视图。然而,我现在需要以编程方式执行此操作,这意味着我需要能够提取当前的 Alter 视图脚本,进行修改,然后针对数据库执行它。最后两个步骤很简单,但第一部分给我带来了一些麻烦。
抛开设计决策不谈(因为在这个特殊情况下它们不在我的控制范围内)。我想知道如何检索 Sql Server Management Studio 用于 View->Edit 命令的 Alter view 脚本。
我需要与该命令完全相同的输出,因为我的脚本中有注释挂钩,允许我进行编辑。
相关问题,但不完全是我想要的。
如何以编程方式检索 SQL服务器存储过程源与 SQL Server Management Studio gui 返回的源相同吗?
We allow our uses to alter certain views for reports and what not based on some application field meta data that we keep track of in our application. These fields can be created at run time. I have a standard process in place to alter the views when a field is added or removed. I now need to do this programmatically however, which means I need to be able to pull the current Alter view script, make my modifications, and then execute it against the database. The last two steps are easy enough, but the first part is giving me some trouble.
Design decisions aside (as they are out of my hands in this particular instance). I would like to know how to retrieve the Alter view script that Sql server management studio uses for the View->Edit command.
I require the exact same output as that command because I have comment hooks in my scripts that allow my edits to occur.
Related questions, but not quite what I am looking for.
In MSSQL, how do I generate a CREATE TABLE statement for a given table?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
SSMS 在幕后使用 SMO。 Scripter 可以检索以下定义任何 SQL 对象。
如果您想要基于 T-SQL 的方法,请使用 sys.sql_modules ,它包含数据库中每个非加密对象(包括视图)的 T-SQL 定义。
SSMS uses SMO under the covers. The Scripter can retrieve the definition of any SQL object.
If you want a T-SQL based approach, then use sys.sql_modules, it contains the T-SQL definition of every non-encrypted object in the database, including views.
您可以从 Information_Schema.Views 表中获取视图创建脚本:
您可以从那里更改它以根据需要构建您的 ALTER。
You can get the View create script from the Information_Schema.Views table:
From there you can alter it to build your ALTER however you need.
我想我终于有了这个问题的答案。通过查看 SQL 探查器,我发现了一个查询,并将其修改为以下内容。
这会将我的视图脚本返回为包含注释的“创建视图”ddl 语句。然而它在一行上,所以我必须在任何注释后插入换行符。我们必须看看这有多可行。
I think I have the answer to this finally. Poking around the SQL profiler I came across a query that I modified to the following.
This returns my view script as a 'Create View' ddl statement complete with comments. It is on one line however, so I have to insert newlines after any comments. We will have to see how feasible this turns out to be.