如何以编程方式检索 SQL Server 2005 中视图的更改视图脚本

发布于 2024-08-12 01:19:05 字数 747 浏览 12 评论 0原文

我们允许我们的用户更改报告的某些视图,以及不基于我们在应用程序中跟踪的某些应用程序字段元数据的内容。这些字段可以在运行时创建。我有一个标准流程来在添加或删除字段时更改视图。然而,我现在需要以编程方式执行此操作,这意味着我需要能够提取当前的 Alter 视图脚本,进行修改,然后针对数据库执行它。最后两个步骤很简单,但第一部分给我带来了一些麻烦。

抛开设计决策不谈(因为在这个特殊情况下它们不在我的控制范围内)。我想知道如何检索 Sql Server Management Studio 用于 View->Edit 命令的 Alter view 脚本。

我需要与该命令完全相同的输出,因为我的脚本中有注释挂钩,允许我进行编辑。

相关问题,但不完全是我想要的。

如何以编程方式检索 SQL服务器存储过程源与 SQL Server Management Studio gui 返回的源相同吗?

在 MSSQL 中,如何为给定表生成 CREATE TABLE 语句?

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.

How do I programmatically retrieve SQL Server stored procedure source that is identical to the source returned by the SQL Server Management Studio gui?

In MSSQL, how do I generate a CREATE TABLE statement for a given table?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凉栀 2024-08-19 01:19:05

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.

做个少女永远怀春 2024-08-19 01:19:05
SELECT [definition] 
FROM sys.sql_modules
WHERE [object_id] = OBJECT_ID('dbo.' + @ViewName);

-- you don't need the type check if you are passing in the name of a view
-- you can do the schema check using OBJECT_ID instead of an extra join

-- if you want to reference system_sql_modules it is unlikely they
-- will have dbo schema, if you want to support this use a separate query IMHO
SELECT [definition] 
FROM sys.sql_modules
WHERE [object_id] = OBJECT_ID('dbo.' + @ViewName);

-- you don't need the type check if you are passing in the name of a view
-- you can do the schema check using OBJECT_ID instead of an extra join

-- if you want to reference system_sql_modules it is unlikely they
-- will have dbo schema, if you want to support this use a separate query IMHO
不寐倦长更 2024-08-19 01:19:05

您可以从 Information_Schema.Views 表中获取视图创建脚本:

SELECT VIEW_DEFINITION FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME='<view name>'

您可以从那里更改它以根据需要构建您的 ALTER。

You can get the View create script from the Information_Schema.Views table:

SELECT VIEW_DEFINITION FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME='<view name>'

From there you can alter it to build your ALTER however you need.

吃→可爱长大的 2024-08-19 01:19:05

我想我终于有了这个问题的答案。通过查看 SQL 探查器,我发现了一个查询,并将其修改为以下内容。

DECLARE @ViewName VARCHAR(255)

SET @ViewName = N'vwMyView'

SELECT
    ISNULL(smv.definition, ssmv.definition) AS [Definition]
FROM
    sys.all_views AS v
    LEFT OUTER JOIN sys.sql_modules AS smv ON smv.object_id = v.object_id
    LEFT OUTER JOIN sys.system_sql_modules AS ssmv ON ssmv.object_id = v.object_id
WHERE
    (v.type = 'V') and (v.name = @ViewName and SCHEMA_NAME(v.schema_id) = N'dbo')

这会将我的视图脚本返回为包含注释的“创建视图”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.

DECLARE @ViewName VARCHAR(255)

SET @ViewName = N'vwMyView'

SELECT
    ISNULL(smv.definition, ssmv.definition) AS [Definition]
FROM
    sys.all_views AS v
    LEFT OUTER JOIN sys.sql_modules AS smv ON smv.object_id = v.object_id
    LEFT OUTER JOIN sys.system_sql_modules AS ssmv ON ssmv.object_id = v.object_id
WHERE
    (v.type = 'V') and (v.name = @ViewName and SCHEMA_NAME(v.schema_id) = N'dbo')

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文