重构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 ...
随机推荐
- 又踩.NET Core的坑:在同步方法中调用异步方法Wait时发生死锁(deadlock)
之前在将 Memcached 客户端 EnyimMemcached 迁移 .NET Core 时被这个“坑”坑的刻骨铭心(详见以下链接),当时以为只是在构造函数中调用异步方法(注:这里的异步方法都是指 ...
- Linux环境下部署完JDK后运行一个简单的Java程序
前言 前一篇文章详细讲解了如何在Windows环境下安装虚拟机+Linux系统,并且成功部署了JDK. 不过部署完JDK之后,我们判断部署是否成功的依据是看"java -version&qu ...
- 《HiWind企业快速开发框架实战》(1)框架的工作原理
<HiWind企业快速开发框架实战>(1)框架的工作原理 1.HiWind架构 HiWind的基本架构如下: 持久层部分:同时为框架本身的业务服务,也为开发人员的自定义业务服务. 逻辑层: ...
- 当程序以Windows Services形式启动时当前路径不对
当程序以Windows Services形式启动时当前路径不对 @(操作系统)[博客|dotNet] 很多时候我们需要将我们的程序写成利用Windows服务的形式来让它能够自启动.今天遇到一个问题,当 ...
- <canvas>drawImage()方法无法显示图片
在书上看到用<canvas>绘制图像就动手试试,刚开始,我的代码是这样的: <!DOCTYPE html> <html> <head> <meta ...
- Base64编码原理分析
Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,在了解Base64编码之前,先了解几个基本概念:位.字节. 位:"位(bit)"是计算机中最小的数据单位.每一位 ...
- JavaScript随笔1
1.NaN不等于NaN 2.判断是不是NaN:isNaN; (强制类型转换) 3.parseInt(3.5) ->3 parseInt(3px)->3 4.pareFloat(3.7)- ...
- PHP中的list()说明
list() 用于在一次操作中给一组变量赋值. 注释:list()只用于数字索引的数组,且假定数字索引从 0 开始. 说明 list() 用数组中的元素为一组变量赋值. 注意,与 array() 类似 ...
- SikuliX简介及安装
一.简单介绍 SikuliIDE和Sikuli Script就是现在的SikuliX,最新版本是SikuliX1.1.0, 部分兼容Sikuli JAVA API,支持Python和Ruby,Siku ...
- 谈谈service层在mvc框架中的意义和职责
mvc框架由model,view,controller组成,执行流程一般是:在controller访问model获取数据,通过view渲染页面. mvc模式是web开发中的基础模式,采用的是分层设计, ...