我们的存储过程包含开发人员评论和标题,作为部署过程的一部分,我们希望从客户副本中删除这些内容。有没有在SQL Server 2005或其他工具中实现此目的的方法?
答案 0 :(得分:2)
我使用名为WinSQL的SQL工具(非常方便,高度推荐),可以选择“在本地解析注释”。
我个人并没有太多使用它,但是我在运行构建我的存储过程的脚本时意外地使用它,它确实将它们从数据库中的proc源清除。 : - )
即使免费版也有这个选项。
答案 1 :(得分:2)
不知道它是否适合,但您可以使用WITH ENCRYPTION选项隐藏整个内容。您的最终用户是否需要查看/修改任何程序?
答案 2 :(得分:2)
当提出问题时,此选项不可用,但在SQL 2012中,我们现在可以使用SQL Server自己的解析器来帮助我们。 Removing Comments From SQL
答案 3 :(得分:1)
您可能需要查看此内容:
Remove Comments from SQL Server Stored Procedures
注意:这不会处理以 - 开头的注释 - ,SQL Server允许的注释。否则,我会询问让开发人员编写一个简短的过滤器应用程序,通过流读取文本,然后以这种方式删除注释。或者自己写。
答案 4 :(得分:1)
我最后在C#中编写了自己的SQL comment remover
答案 5 :(得分:1)
派对有点晚,但万一其他人偶然发现......
CREATE FUNCTION [usf_StripSQLComments] ( @CommentedSQLCode VARCHAR(max) )
RETURNS Varchar(max)
/****************************************************************************************
--######################################################################################
-- Mjwheele@yahoo.com -- 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) =@CommentedSQLCode
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
答案 6 :(得分:0)
我假设您将过程定义保存到版本控制的文本或.sql文件中。您总是可以使用notepadd++之类的内容来查找/替换您想要的字符串,然后将它们作为生产/客户标记提交。这不是优雅,而是一种选择。我不知道任何第三方工具,我的谷歌搜索返回的结果与其他海报发布的结果相同。
答案 7 :(得分:0)
您可以使用C#中的正则表达式删除注释,如here所述。它适用于行注释,块注释,即使嵌套块注释也是如此,并且当它们位于文字或括号内的命名标识符时,它可以正确地识别和忽略注释分隔符。
答案 8 :(得分:0)
这是一个用于删除 SQL 注释的 VB.NET 代码 假设脚本在 SQL Management Studio 下的语法格式良好
Module Module1
Const TagBeginMultiComent = "/*"
Const TagEndMultiComent = "*/"
Const TagMonoComent = "--"
Public Fail As Integer
Function IsQuoteOpened(ByVal Value As String) As Boolean
Dim V As String = Replace(Value, "'", "")
If V Is Nothing Then Return 0
Return ((Value.Length - V.Length) / "'".Length) Mod 2 > 0
End Function
Function RemoveComents(ByVal Value As String) As String
Dim RetVal As String = ""
Dim Block As String
Dim Tampon As String
Dim NbComentIncluded As Integer = 0
Dim QuoteOpened As Boolean
Dim CommentOpen As Boolean
While Value.Length > 0
Tampon = ""
Block = ""
Dim P1 As Integer = InStr(Value, TagBeginMultiComent)
Dim P2 As Integer = InStr(Value, TagEndMultiComent)
Dim P3 As Integer = InStr(Value, TagMonoComent)
Dim Min As Integer
If P1 = 0 Then P1 = Value.Length + 1
If P2 = 0 Then P2 = Value.Length + 1
If P3 = 0 Then P3 = Value.Length + 1
Tampon = ""
If P1 + P2 + P3 > 0 Then
Min = Math.Min(P1, Math.Min(P2, P3))
Tampon = Left(Value, Min - 1)
Block = Mid(Value, Min, 2)
Value = Mid(Value, Min)
End If
If NbComentIncluded = 0 Then QuoteOpened = IsQuoteOpened(RetVal & Tampon)
If Not QuoteOpened Then
NbComentIncluded += -(Block = TagBeginMultiComent) + (Block = TagEndMultiComent)
If Block = TagMonoComent Then
Dim Ploc As Integer = InStr(Value, vbCrLf)
If Ploc = 0 Then
Value = ""
Else
Value = Mid(Value, Ploc - 2)
End If
End If
End If
If Not CommentOpen And NbComentIncluded = 0 Then
RetVal += Tampon
If ({TagBeginMultiComent, TagEndMultiComent, TagMonoComent}.Contains(Block) And QuoteOpened) Or
(Not {TagBeginMultiComent, TagEndMultiComent, TagMonoComent}.Contains(Block) And Not QuoteOpened) Then RetVal += Block
End If
CommentOpen = (NbComentIncluded > 0)
Value = Mid(Value, 3)
End While
Fail = -1 * (IsQuoteOpened(RetVal)) - 2 * (NbComentIncluded > 0)
If Fail <> 0 Then RetVal = ""
Return RetVal
End Function
Sub Main()
Dim InputFileName = "C:\Users\godef\OneDrive - sacd.fr\DEV\DelComentsSql\test.txt" '"C:\Users\sapgy01\OneDrive - sacd.fr\DEV\DelComentsSql\test.txt"
Dim Script As String = File.ReadAllText(InputFileName)
Dim InputDataArray As String() = Split(Script, vbCrLf)
Script = RemoveComents(Script)
If Fail Then
Console.WriteLine($"Fail : {Fail}")
If Fail And 1 = 1 Then Console.WriteLine("Toutes les quotes ne sont pas refermées")
If Fail And 2 = 2 Then Console.WriteLine("Tous les commentaires multiliqnes ne sont pas refermées")
Else
Console.WriteLine(Script)
End If
Console.ReadKey()
End Sub
End Module
插件:检查未闭合的多行注释和/或未闭合的撇号。
示例:
/* Commentaire principal
Suite du commentaire principal
/* Inclusion de commentaire
Suite du commentaire inclu
*/ suite commentaire principal
continuation commentaire principal
/* mono comentaire tagué multi lignes */
*/ select * from ref
-- mono commentaire
select ref.ref_lbl_code as 'code
de
la
-- ref --
' -- from ref as 'references' -- Fin de séquence
from ref as reference -- Mono commentaire fin de ligne
go -- lance l'exécution
select dbo.ref.REF_LBL_CODE as 'commentaire
/* Mulitlignes sur une ligne dans litteral */'
from ref as 'table_ref'
select ref.ref_lbl_code as 'commentaire
/* Mulitlignes
sur plusieurs lignes
dans litteral */'
from ref as '-- ref_table --'
-- Fin de l'exécution du ' -- script -- '