[英]How to retrieve View definition on Synapse (Azure SQL DW)?
I am new to Synapse (Azure SQL DW).我是 Synapse(Azure SQL DW)的新手。 Currently, the DW has lots of views and I need to modify a handful of them.
目前,DW 有很多视图,我需要修改其中的一些视图。 The issue is that I do not know how the views were created.
问题是我不知道视图是如何创建的。 Is there a query to check the view definition in Synapse or more specifically, the SELECT statement was used to create the view?
是否有查询来检查 Synapse 中的视图定义,或者更具体地说,SELECT 语句用于创建视图?
Kind regards, Ken亲切的问候, 肯
sp_helptext
is not supported in Synapse but you can use the view sys.sql_modules
and its definition
column to get the SQL text. Synapse 不支持
sp_helptext
,但您可以使用视图sys.sql_modules
及其definition
列来获取 SQL 文本。 A simple example, tested in a dedicated SQL pool:一个在专用 SQL 池中测试的简单示例:
SELECT *
FROM sys.sql_modules
WHERE definition Like '%someColumn%'
Main help page here .主要帮助页面在这里。 You can also use the function
OBJECT_DEFINITION
and pass it an object_id
, eg您还可以使用函数
OBJECT_DEFINITION
并将其传递给object_id
,例如
SELECT OBJECT_DEFINITION( object_id ), *
FROM sys.views
WHERE is_ms_shipped = 0;
OBJECT_DEFINITION
is definitely the way to go. See the MS docs . OBJECT_DEFINITION
绝对是通往 go 的途径。请参阅MS 文档。
For example, if you want to find out the create
statement for a view called dbo.example
, you would do the following:例如,如果您想找出名为
dbo.example
的视图的create
语句,您可以执行以下操作:
select object_definition(object_id(N'dbo.example')) as [Trigger Definition];
That's it!就是这样!
SELECT
CONCAT('IF OBJECT_ID(''',ss.[name],'.',o.[name],''') IS NOT NULL DROP ',CASE type WHEN 'V' THEN 'VIEW' WHEN 'IF' THEN 'FUNCTION' WHEN 'FN' THEN 'FUNCTION' WHEN 'P' THEN 'PROCEDURE' END ,' ',ss.[name],'.',o.[name],'~GO~~'
,replace(replace(replace(sm.[definition],CHAR(13), '~'),CHAR(10), '~'),'~~','~'),'~GO~')
FROM sys.objects AS o
JOIN sys.sql_modules AS sm
ON o.object_id = sm.object_id
JOIN sys.schemas AS ss
ON o.schema_id = ss.schema_id
WHERE 1=1
AND o.type = 'V'
It looks like you got your answer already but I struggled with formatting etc. in SSMS and Azure Data Studio so figured I'd share this.看起来您已经得到了答案,但我在 SSMS 和 Azure Data Studio 中遇到了格式等问题,所以我想我会分享这个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.