从SQL查询中删除最后一个单词 [英] remove last word from SQL query
本文介绍了从SQL查询中删除最后一个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用SQL SERVER 2008,我必须从SQL SERVER 2008的查询中删除最后一个"OR"字.
Hi,
I am using SQL SERVER 2008,I have to remove last "OR" word from query in SQL SERVER 2008.
my sample query is--select ID from Emp where city=''abc'' OR pincode=123456 OR
我正在建立动态查询,因此会出现额外的OR.
请帮助我从使用函数的查询中删除此多余的OR(SQL SERVER 2008)
I am building dynamically query therefore extra OR is coming.
Please help me to remove this extra OR from query using function(SQL SERVER 2008)
推荐答案
SET @MyString = SUBSTRING(@MyString, 1, LEN(@MyString)-2)
尝试:
string query = "select ID from Emp where city='abc' OR pincode=123456 OR ";
int index = query.LastIndexOf("OR");
if (index >= 0)
{
query = query.Substring(0, index);
}
在构建查询时,只需使用最终的OR即可正确构建查询!
通常,我建议您使用C#构建查询,因为这样做可能更容易进行,也可以验证查询.
无论如何,请记住,当用户打开安全漏洞时,应该从不从用户指定的字符串构建查询.
Simply properly build your query without the final OR when you build it!
Typically, I would recommand you to build your query in C# as it is probably easier to do and also validate the query.
Anyway, remember that you should never build a query from string specified by the user as it open security holes.
这篇关于从SQL查询中删除最后一个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文