在下面的代码中,我使用一个变量将一个值列表传递给多个SQL语句(我不能保存在一个表中,因为我没有权限,也不想在所有不同的SQL节中维护这个列表)。
只要所有的值都在一行上,它就可以正常工作。但是因为我有这么多的值,所以我想把它分成多行,并使用连续字符'-‘。
我正在对Oracle 10g运行Oracle SQL Developer 2.1.1.64 (我也在PL/SQL Developer中尝试过,但在那里也失败了)
--=========================================
define subclasses = ('10-1010-10','10-1010-15','10-1010-20', -
'10-1010-25','10-1010-30') --- there are another 60 values...
select item from item_master where ((subclass) in &&subclasses);
Select Price from Item_prices where ((subclass) in &&subclasses);
--=========================================我得到以下错误
ORA-01722: invalid number
01722. 00000 - "invalid number"因为它将代码解析为
select item from item_master where ((subclass) in ('10-1010-10','10-1010-15',
'10-1010-20', -'10-1010-25','10-1010-30'))在SQL....tho中...keeping连续代码'-‘,它确实转到了值的第二行。
如果我去掉'-‘..。它只处理第一行上的值,并解析为
select item from item_master where ((subclass) in ('10-1010-10','10-1010-15','10-1010-20', )..。丢失第二到第n行值(并抛出错误,因为它以w/‘结束,并且没有最后的')')。我该如何解决这个问题?
发布于 2014-04-02 01:26:28
你可以这样做:
column subc new_value subclasses
select q'[('10-1010-10','10-1010-15','10-1010-20',
'10-1010-25','10-1010-30')]' as subc
from dual;现在&subclasses.将包含所有代码。
不,我用的是q'...‘引号语法,以避免必须将数据中的所有引号都放在一起。
发布于 2018-10-27 20:20:46
我注意到您正在尝试将一个字符串变量列表替换到select语句中。您应该重写定义语句,使其成为单个字符串列表,如下所示:
define subclasses = '''10-1010-10'',''10-1010-15'',''10-1010-20'', -
''10-1010-25'',''10-1010-30'''; --- there are another 60 values...-应该可以用作连续字符(请参阅Oracle documentation here)。
现在,当您执行select语句时,您需要编辑WHERE子句,以便格式化它们,以便它将这些值直接插入其中:
Select item from item_master where subclass in (&subclasses);
Select Price from Item_prices where subclass in (&subclasses);这将最终被解释为好像你已经写了:
Select item from item_master
where subclass in ('10-1010-10','10-1010-15','10-1010-20', '10-1010-25','10-1010-30');但是,如果你有很多值,如果你使用SQL(即每个变量不能超过240字节),你可能会遇到limitations for substitution variables。在这种情况下,您可以将变量拆分为多个变量并在SELECT中连接它们,或者,如果您在PL/SQL环境中,则可以创建保存较大数据大小的变量。
https://stackoverflow.com/questions/22790701
复制相似问题