如何在 SQL Server 2008 Management Studio 中获取视图表查询(代码)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20189134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 23:15:27  来源:igfitidea点击:

How to get a view table query (code) in SQL Server 2008 Management Studio

sqlsql-serverview

提问by Sudhan

I have a view in SQL Server 2008 and would like to view it in Management Studio.

我在 SQL Server 2008 中有一个视图,想在 Management Studio 中查看它。

Example:

例子:

--is the underlying query for the view Example_1
select * 
from table_aView 

View name: Example_1

视图名称: Example_1

How to get the query of the corresponding view table (query used to create the view)?

如何获取对应视图表的查询(用于创建视图的查询)?

Thanks in advance

提前致谢

回答by marc_s

In Management Studio, open the Object Explorer.

在 Management Studio 中,打开对象资源管理器。

  • Go to your database
  • There's a subnode Views
  • Find your view
  • Choose Script view as > Create To > New query window
  • 转到您的数据库
  • 有一个子节点 Views
  • 找到你的观点
  • 选择 Script view as > Create To > New query window

and you're done!

你就完成了!

enter image description here

在此处输入图片说明

If you want to retrieve the SQL statement that defines the view from T-SQL code, use this:

如果要从 T-SQL 代码中检索定义视图的 SQL 语句,请使用以下命令:

SELECT  
    m.definition    
FROM sys.views v
INNER JOIN sys.sql_modules m ON m.object_id = v.object_id
WHERE name = 'Example_1'

回答by Pradeep Kumar Prabaharan

Use sp_helptextbefore the view_name. Example:

使用sp_helptextview_name。例子:

sp_helptext Example_1

Hence you will get the query:

因此,您将得到查询:

CREATE VIEW dbo.Example_1
AS
SELECT       a, b, c
FROM         dbo.table_name JOIN blah blah blah
WHERE        blah blah blah

sp_helptext will give stored procedures.

sp_helptext 将给出存储过程。

回答by Max Asinger

Additionally, if you have restricted access to the database (IE: Can't use "Script Function as > CREATE To"), there is another option to get this query.

此外,如果您对数据库的访问受到限制(即:不能使用“Script Function as > CREATE To”),还有另一个选项可以获取此查询。

Find your View > right click > "Design".

找到您的视图 > 右键单击​​ >“设计”。

This will give you the query you are looking for.

这将为您提供您正在寻找的查询。

回答by Rohaan

if i understood you can do the following

如果我理解你可以执行以下操作

Right Click on View Name in SQL Server Management Studio -> Script View As ->CREATE To ->New Query Window

在 SQL Server Management Studio 中右键单击 View Name -> Script View As ->CREATE To ->New Query Window

回答by Brett Schneider

right-click the view in the object-explorer, select "script view as...", then "create to" and then "new query editor window"

右键单击对象资源管理器中的视图,选择“脚本视图为...”,然后“创建到”,然后选择“新建查询编辑器窗口”