3
Note: Based on this post http://improve.dk/archive/2012/08/27/where-does-sql-server-store-the-source-for-stored-procedures.aspx, is very likely that the definition of views are stored (also) in sys.sysobjvalues
system table.
注意:根据这篇文章http://improve.dk/archive/2012/08/27/where-does-sql-server-store-the-source-for-stored-procedures.aspx,很可能是定义视图(也)存储在sys.sysobjvalues系统表中。
The list of all user T-SQL
modules (within SQL Server 2008) can be queried using sys.sql_modules
system view (link). Here, you can find the definitions of user views (column definition
):
可以使用sys.sql_modules系统视图(链接)查询所有用户T-SQL模块的列表(在SQL Server 2008中)。在这里,您可以找到用户视图的定义(列定义):
SELECT QUOTENAME(s.name)+'.'+QUOTENAME(o.name) AS full_object_name,
m.*
FROM sys.sql_modules m
JOIN sys.objects o ON m.object_id=o.object_id
JOIN sys.schemas s ON o.schema_id=s.schema_id
WHERE o.type='V' -- only view objects
ORDER BY full_object_name
If you run EXEC sp_helptext 'sys.sql_modules'
you will get the source code of this system view:
如果运行EXEC sp_helptext'sys.sql_modules',您将获得此系统视图的源代码:
CREATE VIEW sys.sql_modules AS
SELECT object_id = o.id,
definition = object_definition(o.id),
uses_ansi_nulls = sysconv(bit, o.status & 0x40000), -- OBJMOD_ANSINULLS
uses_quoted_identifier = sysconv(bit, o.status & 0x80000), -- OBJMOD_QUOTEDIDENT
is_schema_bound = sysconv(bit, o.status & 0x20000), -- OBJMOD_SCHEMABOUND
uses_database_collation = sysconv(bit, o.status & 0x100000), -- OBJMOD_USESDBCOLL
is_recompiled = sysconv(bit, o.status & 0x400000), -- OBJMOD_NOCACHE
null_on_null_input = sysconv(bit, o.status & 0x200000), -- OBJMOD_NULLONNULL
execute_as_principal_id = x.indepid
FROM sys.sysschobjs o
LEFT JOIN sys.syssingleobjrefs x ON x.depid = o.id AND x.class = 22 AND x.depsubid = 0 -- SRC_OBJEXECASOWNER
WHERE o.pclass <> 100
AND ((o.type = 'TR' AND has_access('TR', o.id, o.pid, o.nsclass) = 1)
OR (type IN ('P','V','FN','IF','TF','RF','IS') AND has_access('CO', o.id) = 1)
OR (type IN ('R','D') AND o.pid = 0))
You can see that this view queries another system object sys.sysschobjs
that, I think, is the system table used to store definition of views
.
您可以看到此视图查询另一个系统对象sys.sysschobjs,我认为它是用于存储视图定义的系统表。
Note 1: Using INFORMATION_SCHEMA.VIEWS
to find definition of a view is not a reliable method because INFORMATION_SCHEMA.VIEWS.VIEW_DEFINITION
column definition is convert(nvarchar(4000), object_definition(object_id))
(max. 4000 chars).
注1:使用INFORMATION_SCHEMA.VIEWS查找视图的定义不是一种可靠的方法,因为INFORMATION_SCHEMA.VIEWS.VIEW_DEFINITION列定义是转换的(nvarchar(4000),object_definition(object_id))(最多4000个字符)。
Note 2: Instead, you should use sys.sql_modules.definition
column: definition = object_definition(o.id)
. If you look at object_definition
function (link) you will see that return type is nvarchar(max)
.
注意2:相反,您应该使用sys.sql_modules.definition列:definition = object_definition(o.id)。如果查看object_definition函数(链接),您将看到返回类型为nvarchar(max)。