当前位置: 代码迷 >> Sql Server >> 简单的 select 有关问题
  详细解决方案

简单的 select 有关问题

热度:14   发布时间:2016-04-25 00:07:59.0
简单的 select 问题
我有一个语句 select IsFirst, IsLast from MyTable where pkID = @pkID 
 (其中IsFirst, IsLast都是bit类型的字段)

我想在搜索不到符合条件的记录时,就伪造一条记录放到查询结果中,比如 返回( 0, 0 )这条记录,该如何做?
------解决方案--------------------
select IsFirst, IsLast from MyTable where pkID = @pkID 
union all
select 0 IsFirst,0 IsLast from mytable where not exists(select 1 from mytable where pkID = @pkID ) 
------解决方案--------------------
select IsFirst, IsLast from MyTable where pkID = @pkID 
 union all
select 0 IsFirst,0 IsLast from (select 1 as n ) a where not exists(select 1 from mytable where pkID = @pkID )  
------解决方案--------------------
select isnull(IsFirst,0),isnull( IsLast,0) from MyTable where pkID = @pkID 
  相关解决方案