sql - SQL if else 子句可以在里面选择吗?

标签 sql sql-server sql-server-2000

我怀疑这是可能的,但可以。

我需要制定一个执行如下操作的 SQL 查询(绝对不允许存储过程):

Select A.valueFromTable -B.ValueFromTable As result
From table as A inner join table as B
on A.ValueID = B.ValueID
Where ValueID =5

但我需要条件:

if (result <0)
Begin
select A As result ....
end
else
select A-B as result ...

即使确认无法完成,我们也将不胜感激。

通过搜索我认为我当前使用的 SQL Server 版本是 8.0

最佳答案

Select 
    CASE
        WHEN A.valueFromTable -B.ValueFromTable < 0 THEN A.valueFromTable 
    ELSE A.valueFromTable -B.ValueFromTable END As result
From table as A inner join table as B
on A.ValueID = B.ValueID
Where ValueID =5

关于sql - SQL if else 子句可以在里面选择吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8317990/

相关文章:

sql - 比较两个 PostgreSQL 列,字符串由斜杠 "/"或圆括号 "( )"分隔

c# - 使用 Visual Studio 和 NUnit 连接到 localdb

SQL Server - 在带有文本和数字的 varchar 字段中的两个数字范围之间进行选择

sql-server-2000 - 主键数据类型无效 [int]

sql-server - 使用 BCP 从 SQL Server 2000 导出图像列

SQL/Excel/VBA - 更新查询 : 'Syntax Error (missing operator) in query expression'

asp.net - 数据库关系查询

java - Hibernate 不会将 float 正确写入 MS SQL 2005 数据库

android - 实现通知系统

sql - 如何区分表中使用相同查找表的两列?