【问题标题】:Remove conditions in Where clause in a SQL query删除 SQL 查询中 Where 子句中的条件
【发布时间】:2017-09-16 16:51:38
【问题描述】:

我有一个要求,我想删除 SQL 查询字符串中“where”子句中的条件。

如果我的查询是

,则此删除应该像这样工作
Select * 
from table1 
where column1 = @c1 and column2 = 10

那么删除后应该是

Select * 
from table1 
where column2 = 10

如果我的 SQL 查询是

Select * 
from table1 
where column1 = @c1

那么删除后应该是

Select * 
from table1

在 C# 中执行此操作的最佳方法是什么?我可以使用正则表达式还是有更好的方法?

【问题讨论】:

  • 反其道而行之; where (column1 = @c1 or @c1 is null) and (column2 = 10).
  • 所以每次要删除where column1 = @c1?
  • 如果“是”是@WhatsThePoint 问题的答案:当有多个语句时,它总是第一个 where 语句吗?
  • 在我看来就像XY problem。如果您描述最初的问题,也许会更有效。
  • 是的@WhatsThePoint

标签: c# sql regex string


【解决方案1】:

这不是最漂亮的方法,但在你的情况下这会解决问题。

return query.Replace((query.IndexOf(" and ") != -1 ? "column1 = @c1 and " : " where column1 = @c1"), string.Empty);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-08
    相关资源
    最近更新 更多