Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL if no rows are returned do this

I have a select statement and I want to say if this select statement does not return any rows then put a '' in every cell. How do I do this?

like image 761
user380432 Avatar asked Sep 13 '10 16:09

user380432


2 Answers

select a, b, c from t
if @@rowcount = 0
    select '' as a, '' as b, '' as c

But make sure you understand that '' may have a different datatype than columns a, b, and c.

like image 144
tenfour Avatar answered Sep 25 '22 17:09

tenfour


Try this -

IF NOT EXISTS ( SELECT 'x' FROM <TABLE> .... )
BEGIN
    -- Your logic goes here
END
like image 33
Sachin Shanbhag Avatar answered Sep 26 '22 17:09

Sachin Shanbhag