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.
Author |
Topic |
snow12
Yak Posting Veteran
74 Posts |
Posted - 2012-07-02 : 12:02:11
|
Hello:I have a store procedure return some results, but also return no results.PROC SP_NAME@name varchar(100) outputselect @name = student from class How to accomplish if return no result, will set to: None.As Always, you help is highly apprecaited. |
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2012-07-02 : 13:03:42
|
set @name = coalesce((select top 1 student from class), 'none')or if you preferselect @name = student from class -- this will end up with one student value from classselect @name = coalesce(@name, 'none')==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
|
|
snow12
Yak Posting Veteran
74 Posts |
Posted - 2012-07-02 : 15:03:45
|
Thanks for the quick response.This procedure is actually concatenate return string.PROC SP_NAME@returnString varchar(100) outputselect @returnString = Coalesce(@returnString + ', ', '') + cast(student as varchar) from classif called this procedure SP_NAME, I got return string: aaaa,abba,sscd,aczxHowever, I have return string is NULL, I want set up if returnString is NULL, get me "none", else return the resultI use select @returnString = Coalesce(@returnString, 'none') + cast(student as varchar) from class did not work.Where I missed. |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2012-07-02 : 17:01:18
|
Concatenating anything with null returns null, so tryselect @returnString = (@returnString + cast(student as varchar),'None')if @returnString is null or student is null, the returnstring will = 'none'JimP.S cast(student as varchar) can only return one valueEveryday I learn something that somebody else already knew |
|
|
snow12
Yak Posting Veteran
74 Posts |
Posted - 2012-07-02 : 17:16:11
|
Jim:Thanks for the quick response.I tried:select @returnString = (@returnString + cast(student as varchar),'None')I got syntax: Incorrect syntax near ','.This procedure is supposed to return concatenated string but if the result is Null, it will return: NoneWhere I missed? Thank you very much for the help. |
|
|
snow12
Yak Posting Veteran
74 Posts |
Posted - 2012-07-03 : 11:41:43
|
Hello:Is anyone who could help me out?This procedure is supposed to return concatenated string but if the result is Null, it will return: None |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2012-07-03 : 12:59:28
|
Presumably your problem is that there is no data in the class table - so the statement does not set a value in @returnString You coould go with two statementsselect @returnString = Coalesce(@returnString + ', ', '') + cast(student as varchar) from classselect @returnString = coalesce(@returnString, 'none')or maybe something like;with cte as(select student = coalesce(cast(student as varchar), 'none') from class -- cater for null studentunion allselect student = 'none' where not exists (select * from class))select @returnString = Coalesce(@returnString + ', ', '') + student from class==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
|
|
|
|
|
|
|