select case when exists( select userid from user where id = @xxx) then (select top 1 userid from user where id = @xxx ) else '默认值' end
结果 我查询的 userid 一定要有一个值
在程序中处理不就可以了,sql语句得不到,就给个默认值。
select case when userid<0 then 默认值 else userid end from user where id = ?
select userid from user where id =@id union select '默认值' as userid from user where not exists(select 1 from user where id =@id)
select case when exists( select userid from user where id = @xxx) then (select top 1 userid from user where id = @xxx ) else '默认值' end这个可以 呵呵 但是 在这个语句中 只能是一个参数 @xxx 那又如何呢?
if exists (select userid from user where id = ?) select userid from user where id = ? else select 你要的默认值
变量就在你程序的代码里面赋值好了 先在程序里面拼接好 然后执行SQL就可以了 不用过程的话
select userid=case when exists (select 1 from user where id=@id) then (select max(userid) from user where id=@id) else 你需要的默认值 end这里特别注意下,如果能找到的USERID不是一个,请使用MIN或者MAX函数。
case when exists( select userid from user where id = @xxx)
then (select top 1 userid from user where id = @xxx )
else '默认值'
end
select
case
when userid<0 then
默认值
else
userid
end
from user where id = ?
select userid from user where id =@id
union
select '默认值' as userid from user where not exists(select 1 from user where id =@id)
case when exists( select userid from user where id = @xxx)
then (select top 1 userid from user where id = @xxx )
else '默认值'
end这个可以 呵呵 但是 在这个语句中 只能是一个参数 @xxx 那又如何呢?
select userid from user where id = ?
else
select 你要的默认值
select userid=case when exists (select 1 from user where id=@id)
then (select max(userid) from user where id=@id)
else 你需要的默认值 end这里特别注意下,如果能找到的USERID不是一个,请使用MIN或者MAX函数。