Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Default values for select

Author  Topic 

karuna
Aged Yak Warrior

582 Posts

Posted - 2005-10-03 : 10:59:33
In my query say, Select col001,col002,col003 from Table1 where col003=somevalue this will return records which matches the criteria.In case if the criteria does not produce any records then I need to have a default value, something like 0 to be returned for each column. Any way its possible?

Karunakaran

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2005-10-03 : 11:07:43
Union a 'defaulted' record with the result set. then take max...

or write the results to a temp table, and then check the count ... if 0 then select defaults... else select resulsts

Corey

Co-worker on children "...when I have children, I'm going to beat them. Not because their bad, but becuase I think it would be fun ..."
Go to Top of Page

mmarovic
Aged Yak Warrior

518 Posts

Posted - 2005-10-03 : 12:30:30
... or do it on the client.
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2005-10-03 : 14:33:37
quote:
Originally posted by mmarovic

... or do it on the client.



true... that should be the first choice ... but I assumed that since they were asking, that is not an option...

Corey

Co-worker on children "...when I have children, I'm going to beat them. Not because their bad, but becuase I think it would be fun ..."
Go to Top of Page

karuna
Aged Yak Warrior

582 Posts

Posted - 2005-10-03 : 23:56:06
Thanks for the ideas...

Karunakaran
Go to Top of Page

jen
Master Smack Fu Yak Hacker

4110 Posts

Posted - 2005-10-04 : 00:48:00
if this is in sproc, you can set the variable to a default value

@field varchar(10)='any value'

it really depends on what you want to select...



--------------------
keeping it simple...
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-10-04 : 00:57:08
How about this?
If Exists(Select * from Table1 where col003=somevalue)
Select col001,col002,col003 from Table1 where col003=somevalue
else
Select 0 as col1 ,0 as col2,0 as col3



Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

karuna
Aged Yak Warrior

582 Posts

Posted - 2005-10-04 : 09:29:59
Its in Stored Proc, if exists solution looks easier....
Karunakaran
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-10-04 : 10:15:41
So which one do you want to follow?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -