繁体   English   中英

替换SQL中出现的表名

[英]Replace table names occurence in SQL

它不是第一次出现。 索引后,我需要第一次出现。 您指向的问题是没有索引的第一次出现

我有一个SQL查询 ,我正在尝试使用String.Replace进行修改,但是我的代码正在替换它找到品牌的地方,如何避免这种情况

string tempString = 
  "Select t0.brandid,t0.cold,t0.colm, from brands t0 where brandid=@value";

我要替换从t0到t0之前的表名称到tempbrands的品牌

    int tempindex1 = tempString.IndexOf("FROM ");
string tempString1 = tempString.Replace("brands", "tempbrands ");

预期结果是Select t0.brandid,t0.cold,t0.colm, from tempbrands t0 where brandid=@value

使用上一个链接的重复项,我对其进行了修改,以指定从何处开始搜索。 我不知道这是您要找的东西吗?

string ReplaceFirst(string text, string search, string replace, string from)
{
     int posFrom= text.ToLower().IndexOf(from.ToLower());
     if (posFrom < 0)
     {
          return text;
     }
     int pos = text.ToLower().IndexOf(search.ToLower(),posFrom);
     if (pos < 0)
     {
          return text;
     }
     return text.Substring(0, pos) + replace + text.Substring(pos + search.Length);
}

用法:

string tempString1 = ReplaceFirst(tempString, "brands", "tempbrands ", "FROM");

编辑

要在两个字符串之间替换字符串,您可以像下面这样修改先前的方法,但是请注意,仅使用字母作为限制是不可能的。 例如,我按注释中的要求使用“ t”作为限制,如果表名包含“ t”,则它将行不通。 您宁愿使用“ t0”:

string ReplaceFirst(string text, string search, string replace, string from, string to)
{

    int posFrom = text.ToLower().IndexOf(from.ToLower());
    if (posFrom < 0)
    {
        return text;
    }
    int posTo = text.ToLower().IndexOf(to.ToLower(),posFrom);
    if (posTo < 0)
    {
        return text;
    }

    int pos = text.ToLower().IndexOf(search.ToLower(), posFrom);
    if (pos < 0 ||pos >posTo)
    {
        return text;
    }
    return text.Substring(0, pos) + replace + text.Substring(pos + search.Length);
}

用法:

string tempString1 = ReplaceFirst(tempString, "brands", "tempbrands ", "FROM","t0");

我怀疑您只想更改第一次出现 您更可能希望在SQL中重命名表 例如,对于( brands => tempbrands brands更改):

  select b1.brandId,
         b2.brandId
    from brands b1, -- both tables should be changed: this
         brands b2  -- ... and this 
   where b1.brandId > b2.brandId and
         b1.colm = b2.colm

您也可以在例如select范围内更改表名称

  select brands.brandId -- <- all these three occurences should be changed: this,
         brands.colm    --  ... this
    from brands         --  ... and this

可以通过正则表达式来实现部分解决方案(对于任意查询都需要SQL解析器的完整解决方案):

  String tempString = 
    "Select t0.brandid,t0.cold,t0.colm, from brands t0 where brandid=@value";

  // let's change just whole words "brands" into "tempbrands"
  String sql = Regex.Replace(tempString, 
                             @"\bbrands\b", 
                             match => "tempbrands",
                             RegexOptions.IgnoreCase);

为什么解决方案只是部分解决方案? SQL是比通常预期的到达语言:

  select /*this brands should be left intact*/
         MyField as 'another brands should be spared',
    from "this strange brands table should be skipped"

简单地做..

string tempString = 
  "Select t0.brandid,t0.cold,t0.colm, from brands t0 where brandid=@value";
tempString = tempString.Replace("from brands", "tempbrands");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM