View_Definition 在 INFORMATION_SCHEMA.VIEWS 中限制为 4000 个字符

View_Definition in INFORMATION_SCHEMA.VIEWS is limited to 4000 characters

我正在通过 运行 此查询备份我的所有视图,并将结果存储在 table:

select 
   TABLE_CATALOG as DBName
 , TABLE_NAME as ViewName
 , VIEW_DEFINITION as ViewDef
 , datalength(VIEW_DEFINITION) as [Length]
 , GETDATE() as ImportDate 
from INFORMATION_SCHEMA.VIEWS
order by DBName, ViewName

但是 VIEW_DEFINITION 列的数据类型设置为 nvarchar(4000),我的一些视图比这长得多 - 所以它们被截断了。

Columns in INFORMATION_SCHEMA.VIEWS

我可以通过某种方式将 VIEW_DEFINITION 列的数据类型更改为 varchar(max) 吗?

由于 INFORMATION_SCHEMA.VIEWS 是一个视图,您可以 运行 EXEC sp_helptext 'information_schema.views' 找出定义。这个returns

CREATE VIEW INFORMATION_SCHEMA.VIEWS
AS
    SELECT DB_NAME() AS TABLE_CATALOG ,
           SCHEMA_NAME(schema_id) AS TABLE_SCHEMA ,
           name AS TABLE_NAME ,
           CONVERT(NVARCHAR(4000), OBJECT_DEFINITION(object_id)) AS VIEW_DEFINITION ,
           CONVERT(VARCHAR(7) ,
                   CASE with_check_option
                        WHEN 1 THEN 'CASCADE'
                        ELSE 'NONE'
                   END) AS CHECK_OPTION ,
           'NO' AS IS_UPDATABLE
    FROM   sys.views;

从那里只需编辑即可获得您需要的内容

SELECT  
 DB_NAME() AS DBName,  
 name  AS ViewName,  
 OBJECT_DEFINITION(object_id)  AS ViewDef,  
 LEN(OBJECT_DEFINITION(object_id)) AS [Length],
 GETDATE() AS ImportDate
FROM  
 sys.views