Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PL/SQL Question

Posted on 2012-03-09
13
Medium Priority
?
428 Views
Last Modified: 2012-03-09
Hello,

I have an ASP.NET (C#.NET 4.0) search form that contains TextBoxes for first name and last name.  I have to query a Oracle database with PL/SQL.  Below is my current query.  If I comment out the ":lastN is null or..." line, then the query will work.  But if this line is left in the query, it does not work.  Any ideas?

string sqlstmt1 = "SELECT FIRSTNAME, MIDDLENAME, LASTNAME "+
        "FROM MyTable " +
        "WHERE " +
        ":firstN is null or (UPPER(FIRSTNAME) LIKE :firstN) AND " +
        ":lastN is null or (UPPER(LASTNAME) LIKE :lastN) " +
        "ORDER BY LASTNAME, FIRSTNAME";

oraCmd.Parameters.Add("firstN", this.FirstNameTextBox.Text.ToString().ToUpper().Trim());
oraCmd.Parameters.Add("lastN", this.LastNameTextBox.Text.ToString().ToUpper().Trim());

Open in new window

0
Comment
Question by:adskarcox
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 4
13 Comments
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 37702984
>>Oracle database with PL/SQL

PL/SQL is Oralce's Procedural Language.  What you have is plain old SQL.

>>Any ideas?

Even though you repeat the name for the bind variables, you need the same number or parameters for the number of bind variables.

I read somewhere once about 'named' bind variables with .Net and Oracle but not sure if you could re-use them.  I'll see if I can find that docuemnt.


Also, the is null doesn't make sense:

If the bind variable is null, doesn't matter to the database.

Change it to something like:

"(UPPER(FIRSTNAME) LIKE :firstN)"

Then if the textbox.value value="" pass in "%".

That is something like (my C# is rusty, in VB it would be IIF):


 (this.FirstNameTextBox.Text.ToString().ToUpper().Trim() = "", "%",this.FirstNameTextBox.Text.ToString().ToUpper().Trim() )
0
 
LVL 59

Expert Comment

by:HainKurt
ID: 37703010
try

        ":firstN is null or (UPPER(FIRSTNAME) LIKE :firstN) AND " +
        ":lastN is null or (UPPER(LASTNAME) LIKE :lastN) " +

>>>>

"((:firstN='') or (UPPER(FIRSTNAME) LIKE :firstN||'%')) AND " +
"((:lastN='') or (UPPER(LASTNAME) LIKE :lastN||'%')) " +
0
 
LVL 59

Expert Comment

by:HainKurt
ID: 37703032
or use this

"(SubStr(UPPER(FIRSTNAME), 1, length(:firstN))=:firstN) AND " +
"(SubStr(UPPER(LASTNAME), 1, length(:lastN))=:lastN) " +
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 

Author Comment

by:adskarcox
ID: 37703035
HainKurt - that resulted in a "Not all variables bound exception."  Here is the code:


string sqlstmt1 = "SELECT FIRSTNAME, MIDDLENAME, LASTNAME "+
        "FROM MyTable " +
        "WHERE " +
        "((:firstN='') or (UPPER(FIRSTNAME) LIKE :firstN||'%')) AND " +
        "((:lastN='') or (UPPER(LASTNAME) LIKE :lastN||'%')) " +
        "ORDER BY LASTNAME, FIRSTNAME";

oraCmd.Parameters.Add("firstN", this.FirstNameTextBox.Text.ToString().ToUpper().Trim());
oraCmd.Parameters.Add("lastN", this.LastNameTextBox.Text.ToString().ToUpper().Trim());
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 37703037
I cannot find the exact reference buy this one talks about bind by name and bind by position.

http://oradim.blogspot.com/2007/08/odpnet-tip-bind-variables-and.html

Anyway, if you pass in the "%" if the textbox is blank, you only need the two parameters so it is really a non-issue.
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 37703039
>>HainKurt - that resulted in a "Not all variables bound exception."  Here is the code:

I already posted why this will not work...  You have 4 bind variables in the SQL and only 2 declared.

I also posted that the logic doesn't make sense.
0
 

Author Comment

by:adskarcox
ID: 37703044
HainKurt - both code samples resulted in a "Not all variables bound exception."   In the form, I tried filling in one textbox and both, but both tries resulted in the same exception.
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 37703052
Why are you not trying my suggestion?  I know it will work...

Change it to something like:

"(UPPER(FIRSTNAME) LIKE :firstN)"

Then if the textbox.value value="" pass in "%".

That is something like (my C# is rusty, in VB it would be IIF):


 (this.FirstNameTextBox.Text.ToString().ToUpper().Trim() = "", "%",this.FirstNameTextBox.Text.ToString().ToUpper().Trim() )
0
 
LVL 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 1000 total points
ID: 37703059
Full code:


string sqlstmt1 = "SELECT FIRSTNAME, MIDDLENAME, LASTNAME "+
        "FROM MyTable " +
        "WHERE " +
        "UPPER(FIRSTNAME LIKE :firstN) AND " +
        "UPPER(LASTNAME LIKE :lastN) " +
        "ORDER BY LASTNAME, FIRSTNAME";

oraCmd.Parameters.Add("firstN",(this.FirstNameTextBox.Text.ToString().ToUpper().Trim() = "", "%",this.FirstNameTextBox.Text.ToString().ToUpper().Trim() ));
oraCmd.Parameters.Add("lastN", (this.LastNameTextBox.Text.ToString().ToUpper().Trim() = "", "%",this.LastNameTextBox.Text.ToString().ToUpper().Trim() ));
0
 
LVL 59

Assisted Solution

by:HainKurt
HainKurt earned 1000 total points
ID: 37703094
fix for suggestion above by sligtwv

string sqlstmt1 = "SELECT FIRSTNAME, MIDDLENAME, LASTNAME "+
        "FROM MyTable " +
        "WHERE " +
        "UPPER(FIRSTNAME) LIKE :firstN AND " +
        "UPPER(LASTNAME) LIKE :lastN " +
        "ORDER BY LASTNAME, FIRSTNAME";
0
 

Author Comment

by:adskarcox
ID: 37703095
slightwv:  I tried your solution and the exception wasn't as bad.  It threw a "missing right parenthesis" exception.  Code is below:


string sqlstmt1 = "SELECT FIRSTNAME, MIDDLENAME, LASTNAME "+
        "FROM MyTable " +
        "WHERE " +
        "UPPER(FIRSTNAME LIKE :firstN) AND " +
        "UPPER(LASTNAME LIKE :lastN) " +
        "ORDER BY LASTNAME, FIRSTNAME";

oraCmd.Parameters.Add("firstN", (this.FirstNameTextBox.Text.ToString().ToUpper().Trim() == "" ? "%" : this.FirstNameTextBox.Text.ToString().ToUpper().Trim() ));
               
oraCmd.Parameters.Add("lastN", (this.LastNameTextBox.Text.ToString().ToUpper().Trim() == "" ? "%" : this.LastNameTextBox.Text.ToString().ToUpper().Trim() ));
0
 

Author Comment

by:adskarcox
ID: 37703118
Final working code is below.  Thanks guys!


string sqlstmt1 = "SELECT FIRSTNAME, MIDDLENAME, LASTNAME "+
        "FROM MyTable " +
        "WHERE " +
        "UPPER(FIRSTNAME) LIKE :firstN AND " +
        "UPPER(LASTNAME) LIKE :lastN " +
        "ORDER BY LASTNAME, FIRSTNAME";

oraCmd.Parameters.Add("firstN", (this.FirstNameTextBox.Text.ToString().ToUpper().Trim() == "" ? "%" : this.FirstNameTextBox.Text.ToString().ToUpper().Trim() ));
               
oraCmd.Parameters.Add("lastN", (this.LastNameTextBox.Text.ToString().ToUpper().Trim() == "" ? "%" : this.LastNameTextBox.Text.ToString().ToUpper().Trim() ));
0
 
LVL 59

Expert Comment

by:HainKurt
ID: 37703120
anyways, try this code (I used AddWithValue not Add!)

string sqlstmt1 = "SELECT FIRSTNAME, MIDDLENAME, LASTNAME "+
        "FROM MyTable " +
        "WHERE " +
        "(SubStr(UPPER(FIRSTNAME), 1, length(:firstN))=:firstN) AND " +
        "(SubStr(UPPER(LASTNAME), 1, length(:lastN))=:lastN) " +
        "ORDER BY LASTNAME, FIRSTNAME";

oraCmd.Parameters.AddWithValue("firstN", this.FirstNameTextBox.Text.ToString().ToUpper().Trim());
oraCmd.Parameters.AddWithValue("lastN", this.LastNameTextBox.Text.ToString().ToUpper().Trim());
0

Featured Post

CHALLENGE LAB: Troubleshooting Connectivity Issues

Goal: Fix the connectivity issue in the lab's AWS environment so that you can SSH into the provided EC2 instance.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question