比如你有一个简单的表,只有一列。IE。# s3 ?6 S9 r' \- U
CREATE TABLE movies (title VARCHAR2(255 BYTE))设置以下数据:8 a: e$ ?" e6 E# c& J1 n# A& e1 ^9 \
INSERT INTO movies (title) VALUES ('Scream');INSERT INTO movies (title) VALUES ('Blair Witch');INSERT INTO movies (title) VALUES ('Friday the 13th');INSERT INTO movies (title) VALUES ('Scary Movie');INSERT INTO movies (title) VALUES ('Hide and Seek');INSERT INTO movies (title) VALUES ('Alien vs Predator');是否有查询或PL / SQL动态执行以下操作(即不需要手动执行每个值 UNION双重选择’尖叫’…”)?" |; R$ I+ d8 h" q
显然,此查询是错误的,但您可以理解: ' {+ a/ W2 g1 \- z( O3 jSelect * from movieswhere title in ('Scream','Scary Movie','Exorcist','Dracula','Saw','Hide and Seek')期待的结果是 WHERE TITLE IN子句中每个值的记录,表中没有记录。IE。. e; W/ U# F- W+ }
'Exorcist''Dracula''Saw " F1 ^4 I! K l解决方案: % o/ ~( s3 ]7 t$ n7 m" ]
假如你用的是10g或者更高的版本可以构建一个CSV将字符串转换为动态表的函数。 % b" Y% @, W- N6 _1 \ T0 F可以这样用:4 W i% d# k6 T0 \4 o
select * from movieswhere title NOT in ( select * from table (string_tokenizer ( 'Scream,Scary Movie,Exorcist,Dracula,Saw,Hide and Seek 这是一个简单的实现,不需要任何其他基础设施:: q2 \3 |% t9 p# ]2 D
SQL> select * from table(sys.dbms_debug_vc2coll('Scream','Scary Movie','Exorcist','Dracula','Saw','Hide and Seek )/ 2 3 4 55 COLUMN_VALUE--------------------------------------------------------------------------------ScreamScary MovieExorcistDraculaSawHide and Seek6 rows selected.SQL>