T-SQL Comment Extractor

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

DECLARE @script VARCHAR(8000), @extractedScript VARCHAR(8000)
SET @script = ''
/*从系统表获取存储过程的脚本*/
SELECT @script = 'select bankid as [bacnk[   ''  /*--*/  '']] from banks  ''*** /**/   ''  '

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

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

/*前一个字符*/
DECLARE @preChar INT
--是否在行--注释符内
SET @InLineCommented = 0
--是否在段注释符内
SET @InSectionCommented = 0
--是否在字符串内
SET @InString = 0
--是否在方括号[]里面
SET @InSquarebrackets = 0

SET @extractedScript = ''

--初始的字符位移
SET @position = 1

--前一个读入的字符
SET @preChar = null

WHILE @position <= DATALENGTH(@script)
BEGIN
    --获取当前字符
    SET @curChar = ASCII(SUBSTRING(@script, @position, 1))

    --段注释符开始
    IF @curChar = ASCII('*') AND @preChar = ASCII('/') AND @InLineCommented = 0 AND @InString = 0 AND @InSquarebrackets = 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 @curChar = ASCII('/') AND @preChar = ASCII('*') AND @InLineCommented = 0 AND @InString = 0 AND @InSquarebrackets = 0
    BEGIN
         SET @InSectionCommented = 0
         SET @preChar = @curChar
         SET @position = @position + 1
         CONTINUE
    END

    --行注释符开始
    IF @curChar = ASCII('-') AND @preChar = ASCII('-') AND @InSectionCommented = 0 AND @InString = 0 AND @InSquarebrackets = 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 @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 @InString = 0 AND @InSquarebrackets = 0 AND (@curChar = ASCII(' ') OR @curChar = 10 OR @curChar =13 OR @curChar = ASCII(' ') OR @curChar = 32)
    BEGIN
         SET @preChar = @curChar
         SET @position = @position + 1
         CONTINUE
    END
   
    IF @curChar = ASCII('''')
    BEGIN
  IF @InString = 0 AND @InSectionCommented = 0 AND @InLineCommented = 0 AND @InSquarebrackets = 0
  BEGIN        
  SET @InString = 1
  END
  ELSE IF @inString = 1
  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
    END

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

    IF @curChar= ASCII(']') AND @InSquarebrackets = 1 AND @InString = 0 AND @InSectionCommented = 0 AND @InLineCommented = 0
    BEGIN
 SET @InSquarebrackets =0
    END
   
    SET @extractedScript = @extractedScript + CHAR(@curChar)
    SET @preChar = @curChar
    SET @position = @position + 1
END

-- print the result script
SELECT @extractedScript

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
软件特色:针对勒索病毒加密的SQL 数据库有特殊的修复算法,可匹配用友,金蝶数据库结构关系. 支持7.0、SQL2000、SQL2005、SQL2008、 SQL2008R2 、SQL2012、 SQL2014、 SQL2016; •SQL数据库修复软件操作简便; •可以修复数据库置疑状态; •可以修复数据库无法附加或附加报错; •可以修复数据表查询错误; •可以修复MDF文件损坏; •可以修复数据库备份文件损坏; •可以修复数据库被恢复后还是坏的; •可以修复一致性错误; •可以修复错误823; •可以修复输入数据有误; •可以修复运算溢出; •可以修复SQL数据库某些应用程序出错; •可以修复并行事务发生死锁; •支持数据库删除数据的恢复; •支持从日志LDF文件恢复删除的记录; 软件特色:针对勒索病毒加密的SQL 数据库有特殊的修复算法,可匹配用友,金蝶数据库结构关系. 支持7.0、SQL2000、SQL2005、SQL2008、 SQL2008R2 、SQL2012、 SQL2014、 SQL2016; •SQL数据库修复软件操作简便; •可以修复数据库置疑状态; •可以修复数据库无法附加或附加报错; •可以修复数据表查询错误; •可以修复MDF文件损坏; •可以修复数据库备份文件损坏; •可以修复数据库被恢复后还是坏的; •可以修复一致性错误; •可以修复错误823; •可以修复输入数据有误; •可以修复运算溢出; •可以修复SQL数据库某些应用程序出错; •可以修复并行事务发生死锁; •支持数据库删除数据的恢复; •支持从日志LDF文件恢复删除的记录;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值