我试图设置一个SQL命令来检查SELECT语句是否返回值。如果没有要返回的行,那么我想打印“无”。
当我编写这样的语句时,它会起作用:
if exists (select * from TEP_Payments_Table where [Project Name] = 'test')
(select * from TEP_Payments_Table where [Project Name] = 'test')
else
(select 'None')
但是,当我添加“ ORDER BY”时,它将失败:
if exists (select top(1) * from TEP_Payments_Table where [Project Name] = 'test' order by payid desc)
(select top(1) * from TEP_Payments_Table where [Project Name] = 'test' order by payid desc)
else
(select 'None')
那是为什么?
重要的是,我在选择中执行TOP(1)以返回最新值。感谢您的帮助。
答案 0 :(得分:2)
很奇怪。 SQL Server不喜欢第二行的select语句周围的括号。这有效:
await IUserMessage.AddReactionAsync(new Emoji("1️⃣"));
await IUserMessage.AddReactionAsync(new Emoji("2️⃣"));
//...
await IUserMessage.AddReactionAsync(new Emoji("7️⃣"));
答案 1 :(得分:1)
EXISTS
只需验证您用WHERE
子句定义的条件,因此无需使用TOP (1)
或ORDER BY
子句:
您可以直接将其表示为:
if exists (select 1 from TEP_Payments_Table where [Project Name] = 'test')
begin
select top (1) *
from TEP_Payments_Table
where [Project Name] = 'test'
order by payid desc
end
else
begin
select 'non'
end