function fDelSQLComment(sText:string):string; var i,iQuotCount,iLen,iAreaCommentCount:Integer; bLineComment,bAreaComment:Boolean; const TMP_REPLACE_STR=#9+#11+#6+#4; begin
for i:=1 to iLen do begin if (not bAreaComment) and (not bLineComment) and (sText[i]=Char(39)) then begin //单引号计数 Inc(iQuotCount); end;
if ((iQuotCount mod 2)=1) then begin //引号内的数据
end else begin //引号,以及引号外的 if bAreaComment then begin //属于"/*"后面的内容
if (i>=2) and (sText[i-1]='*') and (sText[i]='/') then begin Dec(iAreaCommentCount);
if iAreaCommentCount=0 then begin bAreaComment:=False; //区域注释结束 end; end else if (i<iLen) and (sText[i]='/') and (sText[i+1]='*') then begin //在注释中, 又再次遇到了 "/*", 这里就要注意, 要和SQL查询分析器一样的算法 Inc(iAreaCommentCount); end;
Continue; // 区域注释结束符"*/"不用处理,Continue end else if bLineComment then begin //属于"--"后面的内容
if (sText[i]=#13) then begin bLineComment:=False; //换行符了.(当然,这个换行符,也一样要加进去 end else begin //"--"后面的数据,(仍是同一行),可以隐掉 Continue; end; end else begin //还没有"--" bLineComment:=(sText[i]='-') and (i<iLen) and (sText[i+1]='-'); if bLineComment then Continue; //是"--"开始,那我们就直接忽略这个字符了
bAreaComment:=(sText[i]='/') and (i<iLen) and (sText[i+1]='*'); if bAreaComment then begin // 每遇到一个 "/*",就加1, 遇到 "*/",就减1, 以便符合SQL查询分析器的算法 iAreaCommentCount:=1; Continue; //是"/*"开始,那我们就直接忽略这个字符了 end; end;