如何为下面给出的查询编写等效的 SQL case 语句? [英] How to write the equivalent SQL case statement for query given below?
本文介绍了如何为下面给出的查询编写等效的 SQL case 语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的工作查询:
Query = "select Cust_Id,Card_Number,Clients_Title,Address_Current,Phone_Number,Mobile_Number from Customer_New Where 1=1";
try
{
if (txt_title.Text != "")
Query += " and Clients_Title Like '%" + txt_title.Text + "%'";
if (txt_address.Text != "")
Query += " and Address_Current Like '%" + txt_address.Text + "%'";
if (txt_phone.Text != "")
Query += " and Phone_Number Like '%" + txt_phone.Text + "%'";
if (txt_mobile.Text != "")
Query += " and Mobile_Number Like '%" + txt_mobile.Text + "%'";
if (cbo_location.Text != "")
Query += " and AreaLocation Like '%" + cbo_location.Text + "%'";
}
catch { }
在这里,我试图编写其等效的 SQL case 语句.
Here I am attempting to write its equivalent SQL case statement .
SELECT Cust_Id, Clients_Title, Card_Number, Key_Person, Address_Current, Phone_Number, Mobile_Number, AreaLocation
FROM Customer_New
WHERE 1 = CASE WHEN @Clients_Title != " " THEN Clients_Title AND
WHEN @Address_Current != " " THEN Address_Current AND
WHEN @Phone_Number != " " THEN Phone_Number AND
WHEN @Mobile_Number != " " THEN Mobile_Number AND
WHEN @AreaLocation != " " THEN AreaLocation
END
谁能更正我的案例陈述?
Can any one correct my case statement?
推荐答案
SELECT Cust_Id, Clients_Title, Card_Number, Key_Person, Address_Current, Phone_Number, Mobile_Number, AreaLocation
FROM Customer_New
where (CASE
WHEN @Clients_Title != '' THEN Clients_Title=@Clients_Title
ELSE
NULL IS NULL
END)
AND(CASE
WHEN @Address_Current != '' THEN Address_Current =@Address_Current
ELSE
NULL IS NULL
END)
AND(CASE
WHEN @Phone_Number != '' THEN Phone_Number=@Phone_Number
ELSE
NULL IS NULL
END)
AND(CASE
WHEN @Mobile_Number != '' THEN Mobile_Number=@Mobile_Number
ELSE
NULL IS NULL
END)
AND(CASE
WHEN @AreaLocation != '' THEN AreaLocation =@AreaLocation
ELSE
NULL IS NULL
END)
这篇关于如何为下面给出的查询编写等效的 SQL case 语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文