当前位置: 代码迷 >> Sql Server >> 怎么写这样一个SQL语句
  详细解决方案

怎么写这样一个SQL语句

热度:72   发布时间:2016-04-27 20:13:32.0
如何写这样一个SQL语句
select   a   from   table1   where(条件)
如果有记录显示查询出来的记录,如果没有记录显示“无”,如下所示

有记录:                                       无记录:
a                                                     a
----------                                   ----------
a1                                                   无
a2
a3
...

------解决方案--------------------
if exists(select 1 from table1)
select * from table1
else
select '无 ' from table1
------解决方案--------------------
create table table1(a varchar(8))
insert table1 select 'a1 '
union all select 'a2 '
union all select 'a3 '
go

if exists(select * from table1 where a= 'a1 ')
select * from table1 where a= 'a1 '
else
select top 1 '无 ' as a from table1


if exists(select * from table1 where a= 'a4 ')
select * from table1 where a= 'a4 '
else
select top 1 '无 ' as a from table1

drop table table1
  相关解决方案