SQL 视图结构

lsj_zrp 2011-10-27 05:51:09
有没有办法通过SQL语句查询到视图是如何生成的?
谢谢!
...全文
82 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
-晴天 2011-10-27
  • 打赏
  • 举报
回复
只能通过视图创建语句去分析,系统表中只记录两个东西,一是在sysobjects表中记录视图的一些属性,另一个是在syscolumns表中记录视图所查的列的属性.
--小F-- 2011-10-27
  • 打赏
  • 举报
回复
--批量查询
USE MASTER
GO
CREATE proc sp_MSforeachObject
@objectType int=1,
@command1 nvarchar(2000),
@replacechar nchar(1) = N'?',
@command2 nvarchar(2000) = null,
@command3 nvarchar(2000) = null,
@whereand nvarchar(2000) = null,
@precommand nvarchar(2000) = null,
@postcommand nvarchar(2000) = null
as
/* This proc returns one or more rows for each table (optionally, matching @where), with each table defaulting to its
own result set */
/* @precommand and @postcommand may be used to force a single result set via a temp table. */
/* Preprocessor won't replace within quotes so have to use str(). */
declare @mscat nvarchar(12)
select @mscat = ltrim(str(convert(int, 0x0002)))
if (@precommand is not null)
exec(@precommand)
/* Defined @isobject for save object type */
Declare @isobject varchar(256)
select @isobject= case @objectType when 1 then 'IsUserTable'
when 2 then 'IsView'
when 3 then 'IsTrigger'
when 4 then 'IsProcedure'
when 5 then 'IsDefault'
when 6 then 'IsForeignKey'
when 7 then 'IsScalarFunction'
when 8 then 'IsInlineFunction'
when 9 then 'IsPrimaryKey'
when 10 then 'IsExtendedProc'
when 11 then 'IsReplProc'
when 12 then 'IsRule'
end
/* Create the select */
/* Use @isobject variable isstead of IsUserTable string */
EXEC(N'declare hCForEach cursor global for select ''['' + REPLACE(user_name(uid), N'']'', N'']]'') + '']'' + ''.'' + ''['' +
REPLACE(object_name(id), N'']'', N'']]'') + '']'' from dbo.sysobjects o '
+ N' where OBJECTPROPERTY(o.id, N'''+@isobject+''') = 1 '+N' and o.category & ' + @mscat + N' = 0 '
+ @whereand)
declare @retval int
select @retval = @@error
if (@retval = 0)
exec @retval = sp_MSforeach_worker @command1, @replacechar, @command2, @command3
if (@retval = 0 and @postcommand is not null)
exec(@postcommand)
return @retval
GO

这样我们来测试一下:
--获得所有的存储过程的脚本:
EXEc sp_MSforeachObject @command1="sp_helptext '?' ",@objectType=4
--获得所有的视图的脚本:
EXEc sp_MSforeachObject @command1="sp_helptext '?' ",@objectType=2
中国风 2011-10-27
  • 打赏
  • 举报
回复
[每日小知識]_解决SQL Server里sp_helptext输出格式错行问题
http://topic.csdn.net/u/20080804/12/f477b3d8-0017-4ef9-b516-021032be83ce.html
中国风 2011-10-27
  • 打赏
  • 举报
回复
也可用

CREATE VIEW v
AS
SELECT 1 AS ID
go
SELECT text FROM syscomments WHERE ID=OBJECT_ID('v')
唐诗三百首 2011-10-27
  • 打赏
  • 举报
回复
还有object_definition函数.

create view v1
as
select top 10 * from sysobjects
go

select object_definition(object_id('v1')) code

code
---------------------------------
create view v1
as
select top 10 * from sysobjects
AcHerat 2011-10-27
  • 打赏
  • 举报
回复

create view v1
as
select top 10 *
from hy_fz_productTicketInfo
go

exec sp_helptext v1

drop view v1

/************

Text
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
create view v1
as
select top 10 *
from hy_fz_productTicketInfo
AcHerat 2011-10-27
  • 打赏
  • 举报
回复
sp_helptext
gw6328 2011-10-27
  • 打赏
  • 举报
回复
sp_helptext

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧