SQL Query Using Cursor and table variable(使用游标和表变量的 SQL 查询)
问题描述
输入表有数据,结果表读取输入表的所有数据,检查Name列的长度,打印结果表中<=5的长度
Input table is having data and result table should read all the data from input table and check the length of Name column and should print the length which is <=5 in result table
在此处输入图片说明在此处输入图片描述
你能否建议使用循环(IF else)......比如使用条件读取数据,即<=5,并通过插入语句将其存储在临时表变量中,并通过使用最终选择语句我们得到输出...请帮忙
can you please suggest using loops (IF else) ...like reading the data using conditions i.e. <=5, and by insert statement storing it in a temporary table variable and by using final select statement we are getting the output... plz help
推荐答案
只需使用 LEN() 函数和 case 表达式:
Just use LEN() function with case expression :
select t.id,
(case when len(name) <= 5
then concat(name, '_', len(name))
else name
end) as name,
age
from table t
这篇关于使用游标和表变量的 SQL 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用游标和表变量的 SQL 查询
基础教程推荐
- MySQL 中的类型:BigInt(20) 与 Int(20) 2021-01-01
- 在多列上分布任意行 2021-01-01
- 二进制文件到 SQL 数据库 Apache Camel 2021-01-01
- 在 MySQL 中:如何将表名作为存储过程和/或函数参数传递? 2021-01-01
- 如何在 SQL 中将 Float 转换为 Varchar 2021-01-01
- 表 './mysql/proc' 被标记为崩溃,应该修复 2022-01-01
- oracle区分大小写的原因? 2021-01-01
- 什么是 orradiag_<user>文件夹? 2022-01-01
- mysql选择动态行值作为列名,另一列作为值 2021-01-01
- 如何根据该 XML 中的值更新 SQL 中的 XML 2021-01-01
