c# - 基于行数据的更新查询中的case语句
问题描述
我有一个具有以下结构的表:
ID int (primary key)
SectionName varchar(50)
Allowed bit
例如有5个部分:
ID SectionName Allowed
-----------------------------
1 Basement 1
2 First Floor 1
3 Second Floor 1
4 Third Floor 1
5 Parking 1
我有一个 C# 应用程序,它将调用一个存储过程并填充以下参数
@Basement = false
@First_Floor = true
@Second_Floor = true
@Third_Floor = false
@Parking = true
在执行存储过程的结果后,我希望这些值看起来像
ID SectionName Allowed
----------------------------
1 Basement 0
2 First Floor 1
3 Second Floor 1
4 Third Floor 0
5 Parking 1
我将如何在 Microsoft SQL Server 中创建此存储过程。
解决方案
情况可能是
case
when sectionName = 'Basement' then iif(@Basement = true, 1,0)
when sectionName = 'First Floor' then iif(@First_Floor = true, 1,0)
when sectionName = 'Second Floor' then iif(@Second_Floor = true, 1,0)
when sectionName = 'Third Floor' then iif(@Third_Floor = true, 1,0)
when sectionName = 'Parking' then iif(@Parking = true, 1,0)
end
你可以使用更新
update your_table
set allowed = case
when sectionName = 'Basement' then iif(@Basement = true, 1,0)
when sectionName = 'First Floor' then iif(@First_Floor = true, 1,0)
when sectionName = 'Second Floor' then iif(@Second_Floor = true, 1,0)
when sectionName = 'Third Floor' then iif(@Third_Floor = true, 1,0)
when sectionName = 'Parking' then iif(@Parking = true, 1,0)
end
推荐阅读
- google-kubernetes-engine - 在哪里可以找到 ILB 上的节点端口信息,该信息将流量发送到使用节点端口公开的 GKE 服务
- sql - 检查一周的日期是否位于两个特定日期之间 SQL ORACLE
- sql - 如何将 SQL Server 函数的返回值分配给变量
- c++ - 如何创建格式规则以在打开大括号之前没有空格?
- django - 我可以在 VueJS 中检索请求的标头或其内容吗?
- c# - 如何使用来自 UWP 项目的 4 个参数启动 win32 可执行桌面
- flutter - 如何在颤动中显示进度指示器?
- python - 如何使用列表理解从函数中的函数中提取键值?
- c++ - 如何将值输入到已定义的数组中?
- java - 如何在一行中输入多个不同类型的变量?例如 -> 字符串字符串 int 字符串 int