Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
622 views
in Technique[技术] by (71.8m points)

sqlite - How to write the equivalent SQL case statement for query given below?

This is my working query:

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 { }

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think you just want this - no CASE required:

SELECT Cust_Id, Clients_Title, Card_Number, Key_Person, Address_Current, Phone_Number, Mobile_Number, AreaLocation
FROM Customer_New
    WHERE
      (@Clients_Title = '' OR Clients_Title LIKE '%'+@Clients_Title+'%') AND
      (@Address_Current = '' OR Address_Current LIKE '%'+@Address_Current+'%') AND
      (@Phone_Number = '' OR Phone_Number LIKE '%'+@Phone_Number+'%') AND
      (@Mobile_Number = '' OR Mobile_Number LIKE '%'+@Mobile_Number+'%') AND
      (@AreaLocation = '' OR AreaLocation LIKE '%'+@AreaLocation+'%')

Because that at least closely resembles your non-SQL code.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
...