Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

creating query Expand / Collapse
Author
Message
Posted Sunday, November 11, 2012 7:40 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Sunday, April 28, 2013 2:59 AM
Points: 6, Visits: 30
Hi!

I have to create sql query for searching accouts in my database. Parameters for search i enter in textboxes (id,name,last name...).I know how to create that query when i enter all parameters

SqlCommand cmd = new SqlCommand("SELECT * FROM accounts WHERE id=@id2 AND name=@name2 ...")

but problem is because sometimes i will enter all parameters and sometimes i will enter some of these.In some textboxes i will not enter text at all, and in that case query needs to search accounts parametered only with non empty fields.
Post #1383436
Posted Sunday, November 11, 2012 8:50 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 7:36 PM
Points: 32,931, Visits: 26,820
lazarjojic (11/11/2012)
Hi!

I have to create sql query for searching accouts in my database. Parameters for search i enter in textboxes (id,name,last name...).I know how to create that query when i enter all parameters

SqlCommand cmd = new SqlCommand("SELECT * FROM accounts WHERE id=@id2 AND name=@name2 ...")

but problem is because sometimes i will enter all parameters and sometimes i will enter some of these.In some textboxes i will not enter text at all, and in that case query needs to search accounts parametered only with non empty fields.


That's affectionately known as a "Catch All" query and the definitive (IMHO) article on the subject was written by Microsoft Certified Master Gail Shaw and may be found at the following URL:
http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #1383443
Posted Monday, November 12, 2012 1:37 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Sunday, April 28, 2013 2:59 AM
Points: 6, Visits: 30
Thanks!

It's like this in C#.


SqlCommand cmd = new SqlCommand("SELECT * FROM accounts WHERE (id=@id2 OR @id2 is null) AND (name=@name2 or @name2 is null) AND (last_name=@last_name2 or @last_name2 is null)",con);



if (id.Text.ToString() == "") cmd.Parameters.AddWithValue("id2", DBNull.Value);
else cmd.Parameters.AddWithValue("id2", id.Text);
if (name.Text.ToString() == "") cmd.Parameters.AddWithValue("name2", DBNull.Value);
else cmd.Parameters.AddWithValue("name2", ime.Text);
if (last_name.Text.ToString() == "") cmd.Parameters.AddWithValue("last_name2", DBNull.Value);
else cmd.Parameters.AddWithValue("last_name2", last_name.Text);
Post #1383544
Posted Monday, November 12, 2012 10:02 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, January 04, 2013 1:13 AM
Points: 78, Visits: 123
use like

something like.

(name like '%' +@name + '%' name is null)

Post #1383758
Posted Monday, November 12, 2012 10:31 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 7:36 PM
Points: 32,931, Visits: 26,820
lazarjojic (11/12/2012)
Thanks!

It's like this in C#.


SqlCommand cmd = new SqlCommand("SELECT * FROM accounts WHERE (id=@id2 OR @id2 is null) AND (name=@name2 or @name2 is null) AND (last_name=@last_name2 or @last_name2 is null)",con);



if (id.Text.ToString() == "") cmd.Parameters.AddWithValue("id2", DBNull.Value);
else cmd.Parameters.AddWithValue("id2", id.Text);
if (name.Text.ToString() == "") cmd.Parameters.AddWithValue("name2", DBNull.Value);
else cmd.Parameters.AddWithValue("name2", ime.Text);
if (last_name.Text.ToString() == "") cmd.Parameters.AddWithValue("last_name2", DBNull.Value);
else cmd.Parameters.AddWithValue("last_name2", last_name.Text);


Yep... and I'm suggesting that's incorrect for performance.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #1383769
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse