Skip to Content
SQL经典实例(第2版)
book

SQL经典实例(第2版)

by Anthony Molinaro, Robert de Graaf
December 2021
Intermediate to advanced
470 pages
15h 2m
Chinese
Posts & Telecom Press
Content preview from SQL经典实例(第2版)
处理字符串
111
CNT
时,说明已到达最后一个
EMPNO
,这时列表是完整的。
函数
SYS_CONNECT_BY_PATH
会在列表开头添加你指定的分隔符(本例中是逗
号),这可能是你想要的,也可能不是。在本实例的解决方案中,通过调用
函数
LTRIM
将列表开头的逗号删除了。
6.11
 将分隔数据转换为多值
IN
列表
1.
问题
你想将分隔数据传递给
WHERE
子句中的
IN
列表迭代器。请看下面的字符串。
7654,7698,7782,7788
你想在
WHERE
子句中使用这个字符串,但下面的
SQL
以失败告终,因为
EMPNO
是数值列。
select ename,sal,deptno
from emp
where empno in ( '7654,7698,7782,7788' )
这条
SQL
语句之所以执行失败,是因为
EMPNO
为数值列,而
IN
列表包含的是单个字符串
值。你希望这个字符串被视为用逗号分隔的数值列表。
2.
解决方案
从表面上看,
SQL
好像应该能够将分隔字符串视为值列表
,但情况并非如此。遇到位于引
号内的逗号时,
SQL
不可能
知道这意味着引号内的内容是一个多值列表。
SQL
必须将引
号内的所有内容视为单个实体——一个字符串值。你必须将前述字符串拆分成多个
EMPNO
本解决方案的关键是拆分字符串,但不是拆分为单个的字符,而是拆分为有效的
EMPNO
值。
DB2
通过遍历传递给
IN
列表的字符串,可以轻松地将其转换为多行数据。在这里,函数
ROW_NUMBER
LOCATE
SUBSTR
很有用。
1 select empno,ename,sal,deptno ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Java数据科学实战

Java数据科学实战

Michael R. Brzustowicz, PhD
计算机科学导论:跨学科方法

计算机科学导论:跨学科方法

罗伯特 塞奇威克, 凯文 韦恩

Publisher Resources

ISBN: 9787115577962