I'm backing up all of my views by running this query, and storing the results in a 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
But the datatype for the VIEW_DEFINITION column is set to nvarchar(4000) and some of my views are much longer than that - so they're truncating.
Columns in INFORMATION_SCHEMA.VIEWS
Can I change the datatype of the VIEW_DEFINITION column to varchar(max) somehow?
Since INFORMATION_SCHEMA.VIEWS is a view you can run EXEC sp_helptext 'information_schema.views'
to find out the definition. This 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;
From there just edit to get what you need which is
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