简体   繁体   English

在Oracle中将字符串作为查询运行

[英]run string as query in oracle

i got a little problem in Oracle. 我在Oracle中遇到了一个小问题。 I try to create a sequence for generating IDs in a table that already has data in it. 我尝试创建一个序列来在已经有数据的表中生成ID。 I try to use the following anonymous block. 我尝试使用以下匿名块。

declare y varchar2(2000);
BEGIN
  SELECT 'CREATE SEQUENCE ID_SEQ MINVALUE 1 MAXVALUE 9999999999 START WITH ' || (max(ID)+1) || ' INCREMENT BY 1 CACHE 20;' INTO y FROM TEST_TABLE;
  --dbms_output.put_line(y);
  execute immediate y;
end;

I get the following error: 我收到以下错误:

Error report:
ORA-00911: invalid character
ORA-06512: at line 5
00911. 00000 -  "invalid character"

If I execute the value of the y variable it works perfectly. 如果我执行y变量的值,它将运行完美。 I'm using SQL Developer as input interface and working on a 11g r2 Oracle server. 我使用SQL Developer作为输入接口,并在11g r2 Oracle服务器上工作。 I found similar codes where 'INCREMENT BY' parameter were script generated. 我在生成“ INCREMENT BY”参数的脚本中发现了类似的代码。 Can someone explain my error? 有人可以解释我的错误吗?

When you execute immediate , the command you run should not have a semi-colon on the end; 当您execute immediate ,您运行的命令的末尾不应使用分号; that is a command separator in SQL Developer (and SQL*Plus , and other clients), not part of the SQL statement itself. 这是SQL Developer(以及SQL * Plus和其他客户端)中的命令分隔符,而不是SQL语句本身的一部分。

SELECT 'CREATE SEQUENCE ID_SEQ MINVALUE 1 MAXVALUE 9999999999 START WITH '
    || (max(ID)+1) || ' INCREMENT BY 1 CACHE 20' INTO y FROM TEST_TABLE;

This is shown in the examples for plain SQL . 纯SQL示例中显示了这一点。 Just to help confuse you though, if you are using PL/SQL within the dynamic SQL, you do still need semi-colons appropriate to PL/SQL itself - though not the executing / you'd use to run it directly from a client. 不过,这只是为了帮助您混淆,如果您在动态SQL中使用PL / SQL,您仍然需要适合PL / SQL本身的分号-尽管不需要执行/您可以直接从客户端运行它。 This is shown in other examples . 这在其他示例中显示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM