2009-07-01 124 views
2

我们的存储过程包含开发人员注释和标题,并且作为部署过程的一部分,我们希望将这些注释从客户副本中删除。有没有在SQL Server 2005或其他工具中实现这一点的方法?删除SQL注释

+0

请检查该【答案】(http://stackoverflow.com/a/33947706/3606250) – drizin 2015-11-29 19:57:00

回答

1

您可能要检查了这一点:

Remove Comments from SQL Server Stored Procedures

注意:这不处理以 - 开头的注释,SQL Server允许。否则,我会询问让开发人员编写一个简短的过滤器应用程序,通过流读取文本,然后删除评论。或者自己写。

0

我假设您将过程定义保存到文本或.sql文件中,然后再进行版本控制。你总是可以使用像notepadd++这样的东西来找到/替换你想要的字符串,然后将它们作为生产/客户标签提交。这不是优雅的,而是一种选择。我不知道任何第三方工具,我的谷歌搜索返回的结果与其他发布的海报相同。

2

我使用一个名为WinSQL(非常方便,强烈推荐)的SQL工具,它有一个“本地解析注释”选项。

我并没有亲自使用它,但是当我运行构建我的存储过程的脚本时,我意外地发现了它,并且它将它们从数据库中的proc源中清除。 :-)

即使免费版本也有这个选项。

2

不知道它是否适合,但您可以使用WITH ENCRYPTION选项来隐藏整个内容。您的最终用户是否需要查看/修改任何程序?

1

最后我写我在C#

0

您可以使用C#中的正则表达式删除注释,如here所述。它适用于行注释,块注释,即使块注释嵌套,并且它可以正确识别并忽略注释分隔符,当它们位于文字或括号内的命名标识符内时。

1

有点迟到了,但在整个案件别人绊倒......

CREATE FUNCTION [usf_StripSQLComments] (@CommentedSQLCode VARCHAR(max)) 
RETURNS Varchar(max) 
/**************************************************************************************** 
--###################################################################################### 
-- [email protected] -- Some count sheep. Some code. Some write code to count sheep. 
--###################################################################################### 

--############################################################################# 
-- Sample Call Script 
--############################################################################# 
Declare @SqlCode Varchar(Max) 
Declare @objname varchar(max) = 'sp_myproc' 

select @Sqlcode = OBJECT_DEFINITION(t.OBJECT_ID) 
from sys.objects t 
where t.name = @objname 

select dbo.ssf_StripSQLComments(@Sqlcode) 
****************************************************************************************/ 
AS 
BEGIN 

    DECLARE @Sqlcode VARCHAR(MAX) [email protected] 

    Declare @i integer = 0 
    Declare @Char1 Char(1) 
    Declare @Char2 Char(1) 
    Declare @TrailingComment Char(1) = 'N' 
    Declare @UncommentedSQLCode varchar(Max)='' 
    Declare @Whackcounter Integer = 0 
    Declare @max Integer = DATALENGTH(@sqlcode) 
    While @i < @max 
    Begin 
     Select @Char1 = Substring(@Sqlcode,@i,1) 

     if @Char1 not in ('-', '/','''','*') 
     begin 
      if @Char1 = CHAR(13) or @Char1 = CHAR(10) 
       Select @TrailingComment = 'N' 
      Else if not (@Char1 = CHAR(32) or @Char1 = CHAR(9)) and @TrailingComment = 'N' -- Not Space or Tab 
        Select @TrailingComment = 'Y' 

      if @Whackcounter = 0 
       Select @UncommentedSQLCode += @Char1 
      select @i+=1 
     end 
     else 
     begin 
      Select @Char2 = @Char1 
      ,  @Char1 = Substring(@Sqlcode,@i+1,1) 
      If @Char1 = '-' and @Char2 = '-' and @Whackcounter = 0 
      Begin 
       While @i < @Max and Substring(@Sqlcode,@i,1) not in (char(13), char(10)) 
        Select @i+=1 

       if Substring(@Sqlcode,@i,1) = char(13) and @TrailingComment = 'N' 
        Select @i+=1 
       if Substring(@Sqlcode,@i,1) = char(10) and @TrailingComment = 'N' 
        Select @i+=1 
      End 
      else If @Char1 = '*' and @Char2 = '/' 
       Begin 
        Select @Whackcounter += 1 
        ,  @i += 2 
       End 
       else If @Char1 = '/' and @Char2 = '*' 
        Begin 
         Select @Whackcounter -= 1 

         ,  @i += 2 
        End 
        else if @char2 = '''' and @Whackcounter = 0 
         begin 
          Select @UncommentedSQLCode += @char2 
          while Substring(@Sqlcode,@i,1) <> '''' 
          Begin 
           Select @UncommentedSQLCode += Substring(@Sqlcode,@i,1) 
           ,  @i +=1 
          end 
          Select @i +=1 
          ,  @Char1 = Substring(@Sqlcode,@i,1) 
         end 
         else 
         Begin 
          if @Whackcounter = 0 
           Select @UncommentedSQLCode += @Char2 
          Select @i+=1 
         end 
     end 
    End 
    Return @UncommentedSQLCode 
END