473,385 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

optional parameter in sql query

Hi All,

I have a stored proc which looks like this.

Create ....
(
@id int,
@ud int,
@td int=0
)

if @td=0
select bkah from asdf where id=@id and ud=@ud
else
select bkah from asdf where id=@id and ud=@ud and td=@td

---------------------------------
I am wondering if i could replace if condition with the following line

select bkah from asdf where id=@id and ud=@ud
and ( @td<>0 and td>@td )

IS sql server 2000 smart enough not to use the td>@td in the query if
@td is 0
Thanks all

Nov 6 '05 #1
3 4427
parez (ps*****@gmail.com) writes:
I have a stored proc which looks like this.

Create ....
(
@id int,
@ud int,
@td int=0
)

if @td=0
select bkah from asdf where id=@id and ud=@ud
else
select bkah from asdf where id=@id and ud=@ud and td=@td

---------------------------------
I am wondering if i could replace if condition with the following line

select bkah from asdf where id=@id and ud=@ud
and ( @td<>0 and td>@td )

IS sql server 2000 smart enough not to use the td>@td in the query if
@td is 0


This looks a little strange. If you pass @td = 0, the latter query
will not return any rows. Possibly you mean:

select bkah from asdf where id=@id and ud=@ud and
(@td = 0 OR @td = td)

This works, but keep in mind that when SQL Server builds the query
plan. it does so without knowing of the actual value of @td at
time time of the statement. Thus if there is an index on td that
you want to be used when @td is non-zero, you should not do the above.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp

Nov 6 '05 #2
Hi

I want to use the td column in the query only if @td variable is
passed to the stored procedure.

Thannks

Nov 7 '05 #3
parez (ps*****@gmail.com) writes:
I want to use the td column in the query only if @td variable is
passed to the stored procedure.


Then the original pair of queries you had, will work fine.

For a much longer discussion of the more general problem when you have
many of these @td parameters, there is an article on my web site:
http://www.sommarskog.se/dyn-search.html.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp

Nov 7 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: William Ryan | last post by:
I just picked up a copy of John Robbins' debugging book and started to look at disassembled code. Anyway, I hate optional Parameters in VB, but I was checking them out to see what IL is created. ...
21
by: Marc DVer | last post by:
I am trying to create a query that can be loaded as a querydef object but not having to assign values to the parameters if I don't want to. Normally when using a parameter query in VBA my code...
16
by: ad | last post by:
Does C#2.0 support optional parameters like VB.NET: Function MyFunction(Optional ByVal isCenter As Boolean = False)
10
by: deko | last post by:
In VB, I could do this: MyFuncrion(this As String, that As Integer, Optional otherThing As Boolean) do stuff here End Function In C#, I can use "out" to return multiple values from a method,...
14
by: cody | last post by:
I got a similar idea a couple of months ago, but now this one will require no change to the clr, is relatively easy to implement and would be a great addition to C# 3.0 :) so here we go.. To...
2
by: Oenone | last post by:
In our applications, we use the special value of DateTime.MinValue to represent "null dates" throughout all our code. We recently ran into an issue where we wanted an optional date parameter for a...
12
by: pamelafluente | last post by:
Hi guys, In the past I have used several time optional parameters in my function. But Now I am more inclined to think that they are more dangerous than useful, and probably better to be...
0
by: fhtino | last post by:
Hello, I try to explain my problem with a simple example. I have a simple dataset with only one datatable. The table is also very simple: ID (pk), Name and Age. The first query select all the...
2
by: dantebothermy | last post by:
Hi, I need some help. I have a function with a parameter that runs a query. I'd like the parameter to be optional, so that if the user enters nothing or "all" the query will select all records. ...
7
by: jamesclose | last post by:
My problem is this (apologies if this is a little long ... hang in there): I can define a function in VB.NET with optional parameters that wraps a SQL procedure: Sub Test(Optional ByVal Arg1...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.