简体   繁体   English

如何检索 Synapse 上的视图定义(Azure SQL DW)?

[英]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'
  1. This worked for me using SSMS in Azure Synapse for Scalar Functions(FN) , Inline Functions(IF), Procedures(P), and Views(V)这对我在 Azure Synapse 中使用 SSMS 用于标量函数(FN)、内联函数(IF)、过程(P)和视图(V)
  2. Using the query above in SSMS CNTL-D (Query Results to Grid)在 SSMS CNTL-D(Query Results to Grid)中使用上面的查询
  3. copy results to a new query.将结果复制到新查询。
  4. select a tilde character, ~ with \\n (newline), CNTL-H (Replace)选择一个波浪号字符,〜与 \\n(换行符),CNTL-H(替换)
  5. with the find/replace popup focused use "replace using REGX", ALT+E使用查找/替换弹出窗口集中使用“使用 REGX 替换”,ALT+E
  6. Replace All, ALT+A全部替换,ALT+A

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM