挑战:在SQL查询中删除注释 [英] Challenge : remove comment in SQL query

查看:94
本文介绍了挑战:在SQL查询中删除注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种使用任何编程语言的算法,以删除大型SQL Server脚本文本文件中的注释.

I'm looking for an algorithm in any programing language to remove comments in a wide SQL Server script text file.

评论是这样的

  1. 内联:

  1. inline :

    在行的开头
  • a/(带有-tag或/* */tags组)
  • b/位于行尾(带有-标记或/* */tags组)

多行:

    在行的开头
  • a/(带有-tag或/* */tags组)
  • b/位于行尾(带有-标记或/* */tags组)

包含评论:

  • 一个带有/的内嵌注释,其中包括另一个(或许多其他组)-或/* */(或两者都有)
  • b/多行注释,包含/* /组,其中包括另一个(或许多其他组)/ */或-(或两者)
  • a/ inline comment with the -- which include another (or many other group of) -- or /* */ (or both)
  • b/ multiline comment with the group /* / which include another (or many other group of) / */ or -- (or both)

请注意SQL短语中的字符串注释(内联或多行(包含或不包含))

Be careful of comments in string inside a SQL phrase (inline or multiline (with inclusive or not))

例如:从表中选择字段为-注释"

不应删除它们.

祝你好运.

谢谢

推荐答案

您可以在sql服务器中实现

You could do that within sql server using a parser

declare @sql nvarchar(max) = N'
/*
this is a multiline comment
*/

select * --this is another star comment
from sys.objects;


update mytable /*this is an update comment*/
set colA = colB;
';

declare @x xml = cast(dbo.parseSqlToXml(@sql) as xml);

--get all comment tokens
declare @t table (comment nvarchar(max));
insert into @t(comment)
select 
    replace(replace(replace(s.t.value('.', 'nvarchar(max)'), '\r\n', CHAR(13) + CHAR(10)), '\n', char(10)), '\r', char(13))
from @x.nodes('/SqlScript/SqlBatch/Tokens/Token[contains(@type, "COMMENT")]') as s(t);

--update @sql
update @t
set @sql = replace(@sql, comment, '');

--get @sql
select @sql
for xml path('');

这篇关于挑战:在SQL查询中删除注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆