重构sql server的sys.sp_helptext存储




if object_id(N'dbo.usp_helptext', 'P') IS NOT NULL
begin
drop procedure [dbo].[usp_helptext];
end
go create procedure [dbo].[usp_helptext]
(
@objname nvarchar(776)
,@columnname sysname = NULL
,@keeporiginal bit = NULL
)
as
begin
set nocount on set @keeporiginal = ISNULL(@keeporiginal, 1); declare @dbname sysname
,@objid int
,@BlankSpaceAdded int
,@BasePos int
,@CurrentPos int
,@TextLength int
,@LineId int
,@AddOnLen int
,@LFCR int --lengths of line feed carriage return
,@DefinedLength int /* NOTE: Length of @SyscomText is 4000 to replace the length of
** text column in syscomments.
** lengths on @Line, #CommentText Text column and
** value for @DefinedLength are all 255. These need to all have
** the same values. 255 was selected in order for the max length
** display using down level clients
*/
,@SyscomText nvarchar(4000)
,@Line nvarchar(255) select @DefinedLength = 255
select @BlankSpaceAdded = 0 /*Keeps track of blank spaces at end of lines. Note Len function ignores
trailing blank spaces*/
CREATE TABLE #CommentText
(LineId int
,Text nvarchar(255) collate catalog_default) /*
** Make sure the @objname is local to the current database.
*/
select @dbname = parsename(@objname,3)
if @dbname is null
select @dbname = db_name()
else if @dbname <> db_name()
begin
raiserror(15250,-1,-1)
return (1)
end /*
** See if @objname exists.
*/
select @objid = object_id(@objname)
if (@objid is null)
begin
raiserror(15009,-1,-1,@objname,@dbname)
return (1)
end -- If second parameter was given.
if ( @columnname is not null)
begin
-- Check if it is a table
if (select count(*) from sys.objects where object_id = @objid and type in ('S ','U ','TF'))=0
begin
raiserror(15218,-1,-1,@objname)
return(1)
end
-- check if it is a correct column name
if ((select 'count'=count(*) from sys.columns where name = @columnname and object_id = @objid) =0)
begin
raiserror(15645,-1,-1,@columnname)
return(1)
end
if (ColumnProperty(@objid, @columnname, 'IsComputed') = 0)
begin
raiserror(15646,-1,-1,@columnname)
return(1)
end declare ms_crs_syscom CURSOR LOCAL
FOR select text from syscomments where id = @objid and encrypted = 0 and number =
(select column_id from sys.columns where name = @columnname and object_id = @objid)
order by number,colid
FOR READ ONLY end
else if @objid < 0 -- Handle system-objects
begin
-- Check count of rows with text data
if (select count(*) from master.sys.syscomments where id = @objid and text is not null) = 0
begin
raiserror(15197,-1,-1,@objname)
return (1)
end declare ms_crs_syscom CURSOR LOCAL FOR select text from master.sys.syscomments where id = @objid
ORDER BY number, colid FOR READ ONLY
end
else
begin
/*
** Find out how many lines of text are coming back,
** and return if there are none.
*/
if (select count(*) from syscomments c, sysobjects o where o.xtype not in ('S', 'U')
and o.id = c.id and o.id = @objid) = 0
begin
raiserror(15197,-1,-1,@objname)
return (1)
end if (select count(*) from syscomments where id = @objid and encrypted = 0) = 0
begin
raiserror(15471,-1,-1,@objname)
return (0)
end declare ms_crs_syscom CURSOR LOCAL
FOR select text from syscomments where id = @objid and encrypted = 0
ORDER BY number, colid
FOR READ ONLY end /*
** else get the text.
*/
select @LFCR = 2
select @LineId = 1 open ms_crs_syscom fetch next from ms_crs_syscom into @SyscomText while @@fetch_status >= 0
begin
select @BasePos = 1
select @CurrentPos = 1
select @TextLength = LEN(@SyscomText) while @CurrentPos != 0
begin
--Looking for end of line followed by carriage return
select @CurrentPos = CHARINDEX(char(13)+char(10), @SyscomText, @BasePos) --If carriage return found
IF @CurrentPos != 0
begin
/*If new value for @Lines length will be > then the
**set length then insert current contents of @line
**and proceed.
*/
while (isnull(LEN(@Line),0) + @BlankSpaceAdded + @CurrentPos-@BasePos + @LFCR) > @DefinedLength
begin
select @AddOnLen = @DefinedLength-(isnull(LEN(@Line),0) + @BlankSpaceAdded)
insert #CommentText values
( @LineId,
isnull(@Line, N'') + isnull(SUBSTRING(@SyscomText, @BasePos, @AddOnLen), N''))
select @Line = NULL, @LineId = @LineId + 1,
@BasePos = @BasePos + @AddOnLen, @BlankSpaceAdded = 0
end -- 注释系统原来的使用如下修改
--select @Line = isnull(@Line, N'') + isnull(SUBSTRING(@SyscomText, @BasePos, @CurrentPos-@BasePos + @LFCR), N'')
select @Line = isnull(@Line, N'') + isnull(SUBSTRING(@SyscomText, @BasePos, @CurrentPos-@BasePos + (CASE @keeporiginal WHEN 1 THEN @LFCR ELSE 0 END)), N'')
select @BasePos = @CurrentPos+2
insert #CommentText values( @LineId, @Line )
select @LineId = @LineId + 1
select @Line = NULL
end
else
--else carriage return not found
begin
IF @BasePos <= @TextLength
begin
/*If new value for @Lines length will be > then the
**defined length
*/
while (isnull(LEN(@Line),0) + @BlankSpaceAdded + @TextLength-@BasePos+1 ) > @DefinedLength
begin
select @AddOnLen = @DefinedLength - (isnull(LEN(@Line),0) + @BlankSpaceAdded)
INSERT #CommentText VALUES
( @LineId,
isnull(@Line, N'') + isnull(SUBSTRING(@SyscomText, @BasePos, @AddOnLen), N''))
select @Line = NULL, @LineId = @LineId + 1,
@BasePos = @BasePos + @AddOnLen, @BlankSpaceAdded = 0
end
select @Line = isnull(@Line, N'') + isnull(SUBSTRING(@SyscomText, @BasePos, @TextLength-@BasePos+1 ), N'')
if LEN(@Line) < @DefinedLength and charindex(' ', @SyscomText, @TextLength+1 ) > 0
begin
select @Line = @Line + ' ', @BlankSpaceAdded = 1
end
end
end
end FETCH NEXT from ms_crs_syscom into @SyscomText
end IF @Line is NOT NULL
INSERT #CommentText VALUES( @LineId, @Line ) select Text from #CommentText order by LineId CLOSE ms_crs_syscom
DEALLOCATE ms_crs_syscom DROP TABLE #CommentText return (0) -- sp_helptext
end
go




EXEC [sys].[sp_helptext]
@objname = N'sys.fn_get_sql' -- nvarchar(776)
,@columnname = NULL -- sysname
GO EXEC [dbo].[usp_helptext]
@objname = N'sys.fn_get_sql' -- nvarchar(776)
,@columnname = NULL -- sysname
,@keeporiginal = 1 -- bit
GO
IF OBJECT_ID(N'[dbo].[uvm_MyTestView]', 'V') IS NOT NULL
BEGIN
DROP VIEW [dbo].[uvm_MyTestView];
END
GO CREATE VIEW [dbo].[uvm_MyTestView]
AS
SELECT
1 AS N'Col_1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111', 2 AS [Col_2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222]
GO


重构sql server的sys.sp_helptext存储的更多相关文章
- SQL Server 深入解析索引存储(下)
标签:SQL SERVER/MSSQL SERVER/数据库/DBA/索引体系结构/非聚集索引 概述 非聚集索引与聚集索引具有相同的 B 树结构,它们之间的显著差别在于以下两点: 基础表的数据行不按非 ...
- SQL Server 堆表行存储大小(Record Size)
一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 堆表行记录存储格式(Heap) 案例分析(Case) 参考文献(References) 二.背 ...
- SQL Server 深入解析索引存储(非聚集索引)
标签:SQL SERVER/MSSQL SERVER/数据库/DBA/索引体系结构/非聚集索引 概述 非聚集索引与聚集索引具有相同的 B 树结构,它们之间的显著差别在于以下两点: 基础表的数据行不按非 ...
- SQL Server 2014 聚集列存储
SQL Server 自2012以来引入了列存储的概念,至今2016对列存储的支持已经是非常友好了.由于我这边线上环境主要是2014,所以本文是以2014为基础的SQL Server 的列存储的介绍. ...
- SQL Server 深入解析索引存储(中)
标签:SQL SERVER/MSSQL SERVER/数据库/DBA/索引体系结构/堆 概述 本篇文章是关于堆的存储结构.堆是不含聚集索引的表(所以只有非聚集索引的表也是堆).堆的 sys.parti ...
- SQL Server 深入解析索引存储(上)
标签:SQL SERVER/MSSQL SERVER/数据库/DBA/索引体系结构/堆/聚集索引 概述 最近要分享一个课件就重新把这块知识整理了一遍出来,篇幅有点长,想要理解的透彻还是要上机实践. 聚 ...
- SQL Server索引 (原理、存储)聚集索引、非聚集索引、堆 <第一篇>
一.存储结构 在SQL Server中,有许多不同的可用排列规则选项. 二进制:按字符的数字表示形式排序(ASCII码中,用数字32表示空格,用68表示字母"D").因为所有内容都 ...
- SQL Server 表和索引存储结构
在上一篇文章中,我们介绍了SQL Server数据文件的页面类型,系统通过96个字节的头部信息和系统表从逻辑层面上将表的存储结构管理起来,具体到表的存储结构上,SQL Server引入对象.分区.堆或 ...
- SQL Server 深入解析索引存储(堆)
标签:SQL SERVER/MSSQL SERVER/数据库/DBA/索引体系结构/堆 概述 本篇文章是关于堆的存储结构.堆是不含聚集索引的表(所以只有非聚集索引的表也是堆).堆的 sys.parti ...
随机推荐
- C#用扩展方法进行自动生成添加删除对象转换的功能
public static class ExtendedModel { #region 实体类的增删改查 #region 添加 public static string AddStr(this obj ...
- Eclipse上安装GIT插件EGit及使用
一.Eclipse上安装GIT插件EGit Eclipse的版本eclipse-java-helios-SR2-win32.zip(在Eclipse3.3版本找不到对应的 EGit插件,无法安装) E ...
- SCRIPT65535: 意外地调用了方法或属性访问 ie下不兼容 解决
一般有一下几种 $("#id").text("xxx") 改成 $("#id").attr("text"," ...
- 在Visual Studio上开发Node.js程序(2)——远程调试及发布到Azure
[题外话] 上次介绍了VS上开发Node.js的插件Node.js Tools for Visual Studio(NTVS),其提供了非常方便的开发和调试功能,当然很多情况下由于平台限制等原因需要在 ...
- 策划编写一个新的Helper类
https://code.csdn.net/jy02305022/blqw-data 有朋友看见的话给点意见呗
- Bower是什么?
一.简介 Bower是一个客户端技术的软件包管理器,它可用于搜索.安装和卸载如JavaScript.HTML.CSS之类的网络资源.其它一些建立在Bower基础之上的开发工具,如YeoMan和Grun ...
- 说说设计模式~大话目录(Design Pattern)
回到占占推荐博客索引 设计模式(Design pattern)与其它知识不同,它没有华丽的外表,没有吸引人的工具去实现,它是一种心法,一种内功,如果你希望在软件开发领域有一种新的突破,一个质的飞越,那 ...
- Python格式化输出
今天写程序又记不清格式化输出细节了……= =索性整理一下. python print格式化输出. 1. 打印字符串 print ("His name is %s"%("A ...
- HTML5的特性,发展,及使用
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #ffffff } p.p2 { margin: 0.0px 0. ...
- JAVA的静态变量、静态方法、静态类
静态变量和静态方法都属于静态对象,它与非静态对象的差别需要做个说明. (1)Java静态对象和非静态对象有什么区别? 比对如下: 静态对象 ...