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

时间:2018-02-14 15:52:21

标签: tsql nvarchar information-schema system-views

我通过运行此查询并将结果存储在表中来备份我的所有视图:

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)吗?

1 个答案:

答案 0 :(得分:1)

由于INFORMATION_SCHEMA.VIEWS是一个视图,您可以运行EXEC sp_helptext 'information_schema.views'来查找定义。返回

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