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)
 get data with no result set

Author  Topic 

gaauspawcscwcj
Starting Member

29 Posts

Posted - 2009-12-02 : 01:03:19
hi all
i have to get data in A table. if can not get it ( data is not exist in this table) --> set data by input data.

table A :

item1 item2

input data : item3
--> select item1 from A where item2 = item3. if result of this query is nothing --> set item1 = item3

here is my query :
SELECT
CASE
WHEN @@ROWCOUNT = 0 THEN
'item3'
ELSE
item1
END
FROM A WHERE item2 = 'item3'

the result is nothing.
i had tried ISNULL but its not worked :(
nay ideas for helping me
thanks

gaauspawcscwcj

senthil_nagore
Master Smack Fu Yak Hacker

1007 Posts

Posted - 2009-12-02 : 01:26:03
Try this

select top 1 case when
(select count(*) from A where item2 = 'item3') >0 then item1 else 'item3' end
as data
from A

Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled

http://senthilnagore.blogspot.com/
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-12-02 : 01:44:34

declare @item varchar(20)
set @item=(select max(item1) from A where item2 = 'item3')
select coalesce(@item,'item3')

Madhivanan

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

- Advertisement -