| 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? |
 |
|
|
misterraj
Yak Posting Veteran
94 Posts |
Posted - 2009-06-03 : 07:13:12
|
| In a single query. |
 |
|
|
misterraj
Yak Posting Veteran
94 Posts |
Posted - 2009-06-03 : 07:26:07
|
| any one to answer??????? |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-06-03 : 07:47:09
|
function ?a simple exists() will do the jobif exists (select * from table) print 'true'elseprint 'false' KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
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. |
 |
|
|
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. |
 |
|
|
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 |
 |
|
|
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 |
 |
|
|
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.________________________________________________ |
 |
|
|
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. |
 |
|
|
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 |
 |
|
|
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.________________________________________________ |
 |
|
|
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.idwhere sysindexes.indid in (0, 1) ________________________________________________If it is not practically useful, then it is practically useless.________________________________________________ |
 |
|
|
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. |
 |
|
|
|