• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

T-SQL Extractor

发布: 2007-7-02 11:08 | 作者: admin | 来源: | 查看: 12次 | 进入软件测试论坛讨论

领测软件测试网
/******************************************************************************
 * Author: iret
 * Desc: T-SQL Extractor
 *           Extract the comments and blanks and tabs from the SQL statement
 * 为了比较两个存储过程,或者SQL语句是否一致,抽空写了一个可以删除T-SQL 语句中的注释和空格的脚本,挺精致的样子。
 * Created Date: 2004/10/21
 ******************************************************************************/

DECLARE @script VARCHAR(8000), @extractedScript VARCHAR(4000)
SET @script = @#@#
/*从系统表获取存储过程的脚本*/
SELECT @script = @script + [text]
FROM syscomments, sysobjects
WHERE
    syscomments.[id] = sysobjects.[id] AND sysobjects.[name] LIKE @#%campa_AppSegment@#

/*标志符*/
DECLARE @InLineCommented BIT, @InSectionCommented BIT, @InString BIT, @position INT

/*当前字符*/
DECLARE @curChar INT

/*前一个字符*/
DECLARE @preChar INT

SET @InLineCommented = 0
SET @InSectionCommented = 0
SET @InString = 0

SET @extractedScript = @#@#
SET @position = 1
SET @preChar = null

WHILE @position <= DATALENGTH(@script)
BEGIN
    --获取当前字符
    SET @curChar = ASCII(SUBSTRING(@script, @position, 1))
    IF @preChar = ASCII(@#/@#) AND @curChar = ASCII(@#*@#) AND @InLineCommented = 0 AND @InString = 0
    BEGIN
        -- SET the sign in section comment
        SET @InSectionCommented = 1

         --pop the / char
         SET @extractedScript = substring(@extractedScript,1,len(@extractedScript)-1)

         SET @preChar = @curChar
         SET @position = @position + 1
         CONTINUE
    END

    IF @preChar = ASCII(@#*@#) AND @curChar = ASCII(@#/@#) AND @InLineCommented = 0 AND @InString = 0
    BEGIN
         SET @InSectionCommented = 0
         SET @preChar = @curChar
         SET @position = @position + 1
         CONTINUE
    END

    IF @preChar = ASCII(@#-@#) AND @curChar = ASCII(@#-@#) AND @InSectionCommented = 0 AND @InString = 0
    BEGIN
         SET @InLineCommented = 1

         --pop the / char
         SET @extractedScript = substring(@extractedScript,1,len(@extractedScript)-1)

         SET @preChar = @curChar
         SET @position = @position + 1
         CONTINUE
    END

    IF @curChar = ASCII(@#@#@#@#) AND @InString = 0 AND @InSectionCommented = 0 AND @InLineCommented = 0
    BEGIN
         SET @InString = 1
    END

    IF @inString = 1 AND @curChar = ASCII(@#@#@#@#)
    BEGIN
         IF ASCII(SUBSTRING(@script, @position+1, 1))= ASCII(@#@#@#@#)
         BEGIN  
              SET @extractedScript = @extractedScript + @#@#@#@#
              SET @position = @position + 1
        END
        ELSE
         BEGIN
              SET @InString = 0  
         END
    END

    IF @InSectionCommented = 1
    BEGIN
         SET @preChar = @curChar
         SET @position = @position + 1
         CONTINUE
    END

    IF @InLineCommented = 1
    BEGIN
        -- if meets the end of the line set the InLineCommented to false
        IF @curChar = 10 AND @preChar = 13
        BEGIN
           SET @InLineCommented = 0
        END

         SET @preChar = @curChar
         SET @position = @position + 1
         CONTINUE
    END

    IF @curChar = ASCII(@# @#) OR @curChar = 10 OR @curChar =13 OR @curChar = ASCII(@# @#) OR @curChar = 32
    BEGIN
         SET @preChar = @curChar
         SET @position = @position + 1
         CONTINUE
    END

    SET @extractedScript = @extractedScript + CHAR(@curChar)
    SET @preChar = @curChar
    SET @position = @position + 1
END

-- print the result script
SELECT @extractedScript

 

 

 

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备2023014753号-2
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网