case when then else end 可不可以放在where 后面
时间: 2023-12-07 09:39:05 浏览: 77
是的,case when then else end 语句可以放在where子句后面,用于过滤满足特定条件的行。例如,以下是一个使用case when then else end语句放在where子句后面的示例:
```sql
SELECT *
FROM table_name
WHERE
CASE
WHEN column1 = 'value1' THEN 1
WHEN column2 = 'value2' THEN 1
ELSE 0
END = 1;
```
上述示例中,case when then else end语句用于过滤出满足column1等于'value1'或column2等于'value2'的行。
相关问题
update case when then else end用法
update case when then else end 是 SQL 语言中的一种条件更新语句,用于根据条件更新表中的数据。其基本语法如下:
```
UPDATE 表名 SET 列名 = CASE WHEN 条件1 THEN 值1
WHEN 条件2 THEN 值2
...
ELSE 值n
END
WHERE 条件;
```
其中,CASE WHEN THEN ELSE END 是一个条件表达式,用于根据不同的条件返回不同的值。在 UPDATE 语句中,它被用于更新指定列的值。
举个例子,假设有一个学生表(student),其中包含学生的姓名(name)、年龄(age)和性别(gender)三个字段。现在需要将所有女生的年龄加 1,男生的年龄不变。可以使用如下 SQL 语句:
```
UPDATE student SET age = CASE WHEN gender = '女' THEN age + 1 ELSE age END WHERE 1=1;
```
这条语句会将 student 表中所有女生的年龄加 1,男生的年龄不变。
DECLARE @nmSupport NUMERIC(14,4),@nmOutst NUMERIC(14,4),@chPart VARCHAR(16) SELECT @chPart = 'XXX' SELECT @nmOutst = 0 SELECT @nmSupport = 0 UPDATE acdb.PAC06.AC06004A SET nmSupport = CASE WHEN chPart = @chPart THEN CASE WHEN nmQty > @nmOutst THEN @nmOutst ELSE nmQty END ELSE CASE WHEN nmQty > nmOutst THEN nmOutst ELSE nmQty END END, nmOutst = CASE WHEN chPart = @chPart then case when @nmOutst>=@nmSupport then @nmOutst-@nmSupport else 0 end else nmOutst-nmSupport END, @nmOutst = CASE WHEN chPart = @chPart then case when @nmOutst>=@nmSupport then @nmOutst-@nmSupport else 0 end else nmOutst-nmSupport END, @nmSupport = case when chPart = @chPart then case when nmQty > @nmOutst then @nmOutst else nmQty end ELSE CASE WHEN nmQty > nmOutst THEN nmOutst ELSE nmQty END end, @chPart = chPart改成用游标遍历的方式
好的,以下是将代码改写为使用游标遍历的方式:
```
DECLARE @nmSupport NUMERIC(14,4), @nmOutst NUMERIC(14,4), @chPart VARCHAR(16)
-- 声明游标
DECLARE cur CURSOR FOR
SELECT chPart, nmQty, nmOutst, nmSupport
FROM acdb.PAC06.AC06004A
-- 初始化变量
SET @chPart = 'XXX'
SET @nmOutst = 0
SET @nmSupport = 0
-- 打开游标
OPEN cur
-- 循环遍历游标中的每一行数据
FETCH NEXT FROM cur INTO @chPart, @nmQty, @nmOutst, @nmSupport
WHILE @@FETCH_STATUS = 0
BEGIN
-- 更新 nmSupport 和 nmOutst 字段的值
UPDATE acdb.PAC06.AC06004A
SET nmSupport = CASE
WHEN chPart = @chPart
THEN CASE
WHEN nmQty > @nmOutst
THEN @nmOutst
ELSE nmQty
END
ELSE CASE
WHEN nmQty > nmOutst
THEN nmOutst
ELSE nmQty
END
END,
nmOutst = CASE
WHEN chPart = @chPart
THEN CASE
WHEN @nmOutst >= @nmSupport
THEN @nmOutst - @nmSupport
ELSE 0
END
ELSE nmOutst - nmSupport
END
WHERE CURRENT OF cur
-- 更新变量的值
SET @nmOutst = CASE
WHEN chPart = @chPart
THEN CASE
WHEN @nmOutst >= @nmSupport
THEN @nmOutst - @nmSupport
ELSE 0
END
ELSE nmOutst - nmSupport
END
SET @nmSupport = CASE
WHEN chPart = @chPart
THEN CASE
WHEN nmQty > @nmOutst
THEN @nmOutst
ELSE nmQty
END
ELSE CASE
WHEN nmQty > nmOutst
THEN nmOutst
ELSE nmQty
END
END
SET @chPart = chPart
-- 继续遍历下一行数据
FETCH NEXT FROM cur INTO @chPart, @nmQty, @nmOutst, @nmSupport
END
-- 关闭游标
CLOSE cur
DEALLOCATE cur
```
以上代码使用游标遍历了表中的每一行数据,并按原代码的逻辑更新了 nmSupport 和 nmOutst 字段的值。同时,变量的更新也改为了在循环中进行。
阅读全文