[英]Replace table names occurence in SQL
its not first occurence.
它不是第一次出现。 after a index i need to get the first occurence.
索引后,我需要第一次出现。 the question which you are pointing to is the first occurence without index
您指向的问题是没有索引的第一次出现
i have a sql query which i am trying to modify using String.Replace
but my code is replacing where ever it finds brands, how to avoid this 我有一个SQL查询 ,我正在尝试使用
String.Replace
进行修改,但是我的代码正在替换它找到品牌的地方,如何避免这种情况
string tempString =
"Select t0.brandid,t0.cold,t0.colm, from brands t0 where brandid=@value";
I want to replace brands which is table name between from and before t0 to tempbrands 我要替换从t0到t0之前的表名称到tempbrands的品牌
int tempindex1 = tempString.IndexOf("FROM ");
string tempString1 = tempString.Replace("brands", "tempbrands ");
expected result is Select t0.brandid,t0.cold,t0.colm, from tempbrands t0 where brandid=@value
预期结果是
Select t0.brandid,t0.cold,t0.colm, from tempbrands t0 where brandid=@value
Using the previous linked duplicate, i've modified it to specify where to start the search from. 使用上一个链接的重复项,我对其进行了修改,以指定从何处开始搜索。 I don't know if this is what you were looking for:
我不知道这是您要找的东西吗?
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);
}
Usage: 用法:
string tempString1 = ReplaceFirst(tempString, "brands", "tempbrands ", "FROM");
Edit 编辑
To replace the string between two strings you may modify the previous method like the following, but note the using just a letter as a limit is not possible. 要在两个字符串之间替换字符串,您可以像下面这样修改先前的方法,但是请注意,仅使用字母作为限制是不可能的。 For example, i you use "t" as a limit as you ask in the comment, if the table name contains a "t" it's not going to work.
例如,我按注释中的要求使用“ t”作为限制,如果表名包含“ t”,则它将行不通。 You'd rather use "t0":
您宁愿使用“ 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);
}
Usage: 用法:
string tempString1 = ReplaceFirst(tempString, "brands", "tempbrands ", "FROM","t0");
I doubt that you want to change the 1st occurence only; 我怀疑您只想更改第一次出现 ; it's more likely that you want to rename table within SQL.
您更可能希望在SQL中重命名表 。 eg for (
brands
=> tempbrands
change): 例如,对于(
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
you may mant change table names within, say, select
as well 您也可以在例如
select
范围内更改表名称
select brands.brandId -- <- all these three occurences should be changed: this,
brands.colm -- ... this
from brands -- ... and this
A partial solution (complete solution for arbitrary query wants SQL parser ) can be implemented by regular expression : 可以通过正则表达式来实现部分解决方案(对于任意查询都需要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);
Why the solution is only a partial one? 为什么解决方案只是部分解决方案? SQL is reacher language than is's usually expected:
SQL是比通常预期的到达语言:
select /*this brands should be left intact*/
MyField as 'another brands should be spared',
from "this strange brands table should be skipped"
Make it simply.. 简单地做..
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.