想让结果集为:如果查询的结果为空则返回-1;非空则返回结果集;请各位达人路过帮帮忙!!!

xabcxyz 2008-08-20 11:32:22
想让结果集为:如果select top 1 jc from db_log where cd = '0' order by id desc的结果为空则返回-1;非空则返回结果集
我试着写了两条语句都不行,请各位帮帮忙吧!!!!
select case when exists (select * from (select top 1 jc from db_log where cd = '0' order by id desc) C) then C.jc else -1 end as jc

select case when exists (select * from C) then C.jc else -1 end as jc from (select top 1 jc from db_log where cd = '0' order by id desc) C


谢谢!!!

...全文
229 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
BobHe 2008-08-20
  • 打赏
  • 举报
回复
3楼正解
xabcxyz 2008-08-20
  • 打赏
  • 举报
回复
谢谢邹建!!
zoujp_xyz 2008-08-20
  • 打赏
  • 举报
回复
老大的语句好
zjcxc 2008-08-20
  • 打赏
  • 举报
回复
SELECT
ISNULL(
(select top 1 jc from db_log where cd = '0' order by id desc),
-1)
xabcxyz 2008-08-20
  • 打赏
  • 举报
回复
请教HEROWANG :
两次执行(select top 1 jc from db_log where cd = '0' order by id desc) 不影响性能吗?
  • 打赏
  • 举报
回复
if exists( select * from (select top 1 jc from db_log where cd = '0' order by id desc) )
select * from (select top 1 jc from db_log where cd = '0' order by id desc)
else
return -1
jminxuan 2008-08-20
  • 打赏
  • 举报
回复

declare @i int;

Set @i = -1;
Select top1 @i = jc from db_log where cd = '0' order by id desc

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧