【NCRE】初遇 SQL SERVER 的 CASE WHEN
我们的c#中经常用到了switch case,那么在sql中是否也有这样的一种格式或者是功能呢?
CASE WHEN 是sql server中的重要关键字之一,也许你对他有很多的理解,但是他真的有很多的用法,今天我就来向大家介绍一种。
实现简单的搜索功能;
数据库中的表:
现在我们要实现根据专业看各个学院有多少的人。
case when 语句:
实现的效果:
由于我的这个例子数据量很小,所以好像看不出来他的优势。但是这个例子很简单的让我们都了解了他的最基本的语法结构。
当我们的数据量很大,用我们的机房的那种一句sql语句打开一个sqlhelper的方法速度就会很慢的。而用它可以大大的减少时间。
在NCRE中,更新分数和考生答题记录,师哥就是用的这种方法。这是一个D层的代码:
<span style="font-size:18px;"> public void ReturnScore(List<IEQuestionRecordEntity> list, IEQuestionRecordEntity studentrecord){String which = WhichIERecored(studentrecord);StringBuilder sbSql = new StringBuilder();sbSql.Append(" update IEQuestionRecordEntity_" + which + " SET fraction = CASE questionID ");string Fation = string.Empty;string questionID = string.Empty;string studentIDs = string.Empty;string tamstamp = string.Empty;//根据学号,和时间戳选出这一段的内容,然后再根据QuestionID给每一个flag更新分数。#region 更新分数的拼接sql语句for (int i = 0; i < list.Count; i++){questionID = list[i].questionID.ToString();//将分数传给变量Fation = list[i].fraction.ToString();//判分sql,拼接sql,用case,when语句sbSql.Append(" WHEN '" + questionID + "' THEN '" + Fation + "'");}sbSql.Append(" end,"); //加“,” #endregion#region 拼接考生答案更新到数据库中//拼接更新考生答案sqlsbSql.Append(" examAnswer = CASE questionID");for (int i = 0; i < list.Count; i++){string examAnswer = list[i].examAnswer.ToString();questionID = list[i].questionID.ToString();//学生答案信息更新,when后边的是QuerstionID,sbSql.Append(" WHEN '" + questionID + "' THEN '" + list[i].examAnswer + "'");}sbSql.Append(" end"); //不加逗号“,” #endregion studentIDs += "'" + list[0].studentID.ToString() + "'";tamstamp += "'" + list[0].timeStamp.ToString() + "'";studentIDs.Remove(studentIDs.Length - 1, 1);sbSql.Append(" where studentID in (" + studentIDs + ") and timeStamp in (" + tamstamp + ")");DataTable dt = sqlhelper.ExecuteQuery(sbSql.ToString(), CommandType.Text);}</span>
利用循环和CASE WHEN,解决了多次打开SQLHelper的问题。
SQL 提供了巨大的语句,给我们的代码带来了更大程度上的方便。
【NCRE】初遇 SQL SERVER 的 CASE WHEN相关推荐
- sql server中case when的用法
CASE sexWHEN '1' THEN '男'WHEN '2' THEN '女'ELSE '其他' END 注:sql server中case when的用法 - 哦呵呵G - 博客园 --同样的 ...
- Sql Server中Case函数的使用(上篇)----转载
Case具有两种格式.简单Case函数和Case搜索函数. 代码 --简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其他' E ...
- SQL Server:CASE WHEN OREN ELSE END =不支持OR
不支持CASE语句的WHEN子句中的OR . 我怎样才能做到这一点? CASE ebv.db_no WHEN 22978 OR 23218 OR 23219 THEN 'WECS 9500' ELSE ...
- Sql Server where case 满足条件执行 不满足 不做任何条件查询
sql 里经常用的where语句中可以使用case when 常用的 select * from tb where a= case when b>0 and b<100 then 1 wh ...
- sql server中case的简单示例
select *, case when resultrate>0 then case --<<case嵌套 ...
- 在Windows Server 2016和SQL Server Always On可用性组上安装SQL Server 2019
In this article, we will proceed with configuring a SQL Server Always On Availability Groups and per ...
- SQL Server中的部分存储过程
介绍 (Introduction) SQL is an old language - almost 50 years! The first commercially-available version ...
- oracle stdevp函数,SQL Server与oracle两者区别之函数区别
Oracle和SQL Server的常用函数对比 1.数学函数 ①绝对值 S:select abs(-1) value O:select abs(-1) value from dual ②取整(大) ...
- sql安装弹出sqlcmd_使用SQL Server命令行(sqlcmd)
sql安装弹出sqlcmd 介绍 (Introduction) Sqlcmd allows executing queries, T-SQL sentences and SQL Server scri ...
最新文章
- mock 测试 MVC
- php无限加载,php递归无限页面加载
- java liste_内功心法 -- java.util.ArrayListE (1)
- java 安卓权限_java – Android运行时权限 – 如何实现
- 关于阿里云,有什么故事?
- 去伪、存真、打破、重塑……网贷业暴雷潮之下的敬畏与回归
- LaTeX Beamer 制作PPT时给某一页添加背景图片(并设置透明度)
- 理解exponential weighted || 指数
- r如何打开计算机窗口,如何进入运行窗口
- Ubuntu 设置屏幕字体大小
- 艾宾浩斯曲线真的管用吗?
- 通向Golang的捷径【13. 错误处理和测试】
- 【TensorFlow报错】AttributeError: module 'tensorflow' has no attribute 'merge_all_summaries'
- SQL多对多关系通过关联中间表查询
- 如何学习新的IT技术
- mysql zip 版本_MySQL zip版本安装
- 提高网站访问速度的十个技巧
- 学习记录633@python特征筛选之WOE值与IV值
- CANalyzer及CANOE使用二:基于UDS的Panel界面及使用Capl编写BootLoader自动化刷写流程(多文件or多段下载)
- python调用wmts服务_OpenLayers 加载山东天地图(WMTS)