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 2005 Forums
 Transact-SQL (2005)
 count true or false

Author  Topic 

misterraj
Yak Posting Veteran

94 Posts

Posted - 2009-06-03 : 07:02:09

i need a query where if the count of records from a table is >0 it has to return true else false

ayamas
Aged Yak Warrior

552 Posts

Posted - 2009-06-03 : 07:08:16
Do you mean a function?
Go to Top of Page

misterraj
Yak Posting Veteran

94 Posts

Posted - 2009-06-03 : 07:13:12
In a single query.

Go to Top of Page

misterraj
Yak Posting Veteran

94 Posts

Posted - 2009-06-03 : 07:26:07
any one to answer???????
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-06-03 : 07:47:09
function ?

a simple exists() will do the job

if exists (select * from table)
print 'true'
else
print 'false'



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

misterraj
Yak Posting Veteran

94 Posts

Posted - 2009-06-03 : 07:50:54
no functions or stored procs please, just the query. query should return true or false depending if the records are available or not.
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-06-03 : 07:57:01
That is what KH has posted!
If you want to find out if there is at least one record available by your condition:
If exists (select * from table where ...) -- returns true or false


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2009-06-03 : 08:59:21
SELECT HasRecords = CASE WHEN COUNT(*) > 0 THEN 'true' ELSE 'false' END FROM mytable

- Lumbago
Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2009-06-03 : 09:02:35
Probably more effective:

SELECT HasValues = CASE WHEN EXISTS (SELECT * FROM mytable) THEN 'true' ELSE 'false' END

- Lumbago
Go to Top of Page

blindman
Master Smack Fu Yak Hacker

2365 Posts

Posted - 2009-06-03 : 09:30:34
select convert(bit, count(*)) from YourTable

________________________________________________
If it is not practically useful, then it is practically useless.
________________________________________________
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-06-03 : 09:35:44
quote:
Originally posted by blindman

select convert(bit, count(*)) from YourTable

________________________________________________
If it is not practically useful, then it is practically useless.
________________________________________________


that's not bad!


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2009-06-03 : 09:46:36
Even though the convert to bit-method is kinda cool I'd still go with the exists...WAY faster.

- Lumbago
Go to Top of Page

blindman
Master Smack Fu Yak Hacker

2365 Posts

Posted - 2009-06-03 : 12:54:43
...as long as there are no predicates or grouping going on, yes.

________________________________________________
If it is not practically useful, then it is practically useless.
________________________________________________
Go to Top of Page

blindman
Master Smack Fu Yak Hacker

2365 Posts

Posted - 2009-06-03 : 12:56:19
Hackish, but he could supply the tablename via parameter:
select	sysobjects.name,
convert(bit, sysindexes.rowcnt)
from sysobjects
inner join sysindexes on sysobjects.id = sysindexes.id
where sysindexes.indid in (0, 1)


________________________________________________
If it is not practically useful, then it is practically useless.
________________________________________________
Go to Top of Page

misterraj
Yak Posting Veteran

94 Posts

Posted - 2009-06-04 : 00:39:23
thanks blindman and lumbago, they are perfectly working on the way as it is expected to.
Go to Top of Page
   

- Advertisement -