Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
SQL Server 2008
»
SQL Server 2008 - General
»
creating query
creating query
Rate Topic
Display Mode
Topic Options
Author
Message
lazarjojic
lazarjojic
Posted Sunday, November 11, 2012 7:40 AM
Forum 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
Jeff Moden
Jeff Moden
Posted Sunday, November 11, 2012 8:50 AM
SSC-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 "
R
ow-
B
y-
A
gonizing-
R
ow".
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
lazarjojic
lazarjojic
Posted Monday, November 12, 2012 1:37 AM
Forum 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
freecoder
freecoder
Posted Monday, November 12, 2012 10:02 AM
SSC 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
Jeff Moden
Jeff Moden
Posted Monday, November 12, 2012 10:31 AM
SSC-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 "
R
ow-
B
y-
A
gonizing-
R
ow".
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 »
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.