|
|
Subject:
SQL Expert Needed: Complex SQL Select Statement
Category: Computers > Programming Asked by: bdwgarth-ga List Price: $5.00 |
Posted:
07 Jul 2004 08:49 PDT
Expires: 06 Aug 2004 08:49 PDT Question ID: 370865 |
Developing a web application in VB.NET. Looking to create a complex and extensive SQL Select statement. In the application there are four drop down list which correspond to fields in the database. The SELECT statement should return the results from the database of the criteria that the user of the application selected from the drop down list. The problem is how do you not include a portion of the select statement or make a drop down list item not searchable, IF one or more of the drop down list items is NOT selected. Is there a way to ?hide? this value in the Select statement so it does not include it in the search? Or does the Select statement have to be built as a string corresponding to the DDL that are selected or not selected through a series of IF statements. Any suggestions or examples? Examples of Select statements or code would be great. | |
|
|
There is no answer at this time. |
|
Subject:
Re: SQL Expert Needed: Complex SQL Select Statement
From: jkealey-ga on 09 Jul 2004 10:57 PDT |
As I believe dropdownlists always have a selected value (the first one in the list), I suggest that you add an entry in your dropdownlists at index 0 with text="-" or something similar to indicate no selection has been made. Hence, you can build your sql statement as such (C# code) strSQL = "SELECT * FROM tblWhatever WHERE " + (ddl1.SelectedIndex==0?"":"field1=" + ddl1.SelectedValue + " AND ") + (ddl2.SelectedIndex==0?"":"field2=" + ddl2.SelectedValue + " AND ") + "1=1;" The last "1=1" is to ensure that you don't end the where clause with an AND and it evaluates to true for all records. the ? : are imbricated IF statements |
Subject:
Re: SQL Expert Needed: Complex SQL Select Statement
From: jkealey-ga on 09 Jul 2004 11:01 PDT |
For VB.NET (forgot that's what you wanted :)), you could do: strSQL = "SELECT * FROM tblWhatever WHERE "; if ddl1.SelectedIndex=0 then strSQL = strSQL & "field1=" & ddl1.SelectedValue & " AND ") end if if ddl2.SelectedIndex=0 then strSQL = strSQL & "field2=" & ddl2.SelectedValue & " AND ") end if strSQL = strSQL & "1=1;" Obviously, if your fields aren't numeric but are strings, you need to use appropriate quoting. if ddl3.SelectedIndex=0 then strSQL = strSQL & "field3='" & ddl3.SelectedText.Replace("'","''") & "' AND ") end if or something of the like (haven't done VB in a while) |
Subject:
Re: SQL Expert Needed: Complex SQL Select Statement
From: dishanfernando-ga on 15 Jul 2004 23:26 PDT |
Hi., Set Values to your Drop down list say 0 - All 1 - male 2 - female and build your sql according to index value if its not equal to 0 sSQL = "select * from tablename " " where 1=1 "; if( cmbBox1.index != 0 ) sSQL += " and name Like '%" + cmbBox1.value + "%'"; if( cmbBox2.index != 0 ) sSQL += " and address Like '%" + cmbBox2.value + "%'"; hth Dishan |
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you. |
Search Google Answers for |
Google Home - Answers FAQ - Terms of Service - Privacy Policy |