如何使用 CONCAT 解决 SQL Plus 中的 SP2-0734?
Posted
技术标签:
【中文标题】如何使用 CONCAT 解决 SQL Plus 中的 SP2-0734?【英文标题】:How to resolve SP2-0734 in SQL Plus using CONCAT? 【发布时间】:2014-10-27 19:52:27 【问题描述】:我创建了一个生成此错误的脚本,但它仍然可以完美执行。我能做些什么来解决这个错误?我尝试了一些常见的修复方法,例如 SET SQLBLANKLINES ON,但没有奏效。
错误:SP2-0734:以“CONCAT('IN...”开头的未知命令 - 忽略行的其余部分。
这是产生错误的脚本部分:
PROMPT 'ENTER THE PERCENTAGE YOU WISH TO INCREASE THE PRICE BY: '
ACCEPT V_PCT_INCREASE NUMBER
PROMPT
PROMPT 'ENTER THE PRODUCT SEARCH CRITERIA: '
ACCEPT V_PRODUCT_DESC
PROMPT
PROMPT 'YOU HAVE CHOSEN TO UPDATE PRODUCTS WHOSE DESCRIPTION BEGINS WITH &V_PRODUCT_DESC WITH A PERCENTAGE INCREASE OF &V_PCT_INCREASE PERCENT.'
PROMPT
SET TERMOUT OFF;
SET VERIFY OFF;
SET SQLBLANKLINES ON;
SPOOL c:\runscripts\insert_prices.sql;
SELECT CONCAT('INSERT INTO TAR_TEMP_PRICE (PRODUCT_ID, START_DATE, LIST_PRICE, MIN_PRICE, END_DATE)
VALUES (', P.PRODUCT_ID||', '''||PR.START_DATE||''', '||PR.LIST_PRICE * (1+(&V_PCT_INCREASE/100))||', '||PR.MIN_PRICE * (1+(&V_PCT_INCREASE/100))||', CURRENT_DATE'||');')
FROM TAR_PROD P
JOIN TAR_PRICE PR
ON P.PRODUCT_ID = PR.PRODUCT_ID
WHERE UPPER(P.DESCRIPTION) LIKE UPPER('&V_PRODUCT_DESC%');
SPOOL OFF;
SET FEEDBACK ON;
SET TERMOUT ON;
PROMPT 'A SCRIPT insert_prices.sql HAS BEEN CREATED AT c:\runscripts\insert_prices.sql'
PROMPT
PROMPT 'THE FOLLOWING PRODUCTS WERE ADDED TO THE INSERT SCRIPT WITH THE ADJUSTED PRICES: '
SELECT P.PRODUCT_ID, P.DESCRIPTION, PR.LIST_PRICE, PR.MIN_PRICE,
PR.LIST_PRICE * (1+(&V_PCT_INCREASE/100)) AS "ADJ_LIST_PRICE",
PR.MIN_PRICE * (1+(&V_PCT_INCREASE/100)) AS "ADJ_MIN_PRICE"
FROM TAR_PROD P
JOIN TAR_PRICE PR
ON P.PRODUCT_ID = PR.PRODUCT_ID
WHERE UPPER(P.DESCRIPTION) LIKE UPPER('&V_PRODUCT_DESC%');
@C:\runscripts\insert_prices.sql
PROMPT 'INSERT SCRIPT IS COMPLETE.'
PROMPT 'SEE BELOW FOR RESULT VALIDATION.'
SELECT *
FROM TAR_TEMP_PRICE;
ROLLBACK;
它仍然有效,但我想抑制错误。
以上代码生成的插入语句:
CONCAT('INSERTINTOTAR_TEMP_PRICE(PRODUCT_ID,START_DATE,LIST_PRICE,MIN_PRICE,END_DATE)VALUES(',P.PRODUCT_ID||','''||PR.START_DATE||''','||PR.LIST_PRICE
------------------------------------------------------------------------------------------------------------------------------------------------------
INSERT INTO TAR_TEMP_PRICE (PRODUCT_ID, START_DATE, LIST_PRICE, MIN_PRICE, END_DATE)
VALUES (100860, '01-JUN-90', 39.2, 31.36, CURRENT_DATE);
INSERT INTO TAR_TEMP_PRICE (PRODUCT_ID, START_DATE, LIST_PRICE, MIN_PRICE, END_DATE)
VALUES (100860, '01-JAN-90', 35.84, 28.672, CURRENT_DATE);
INSERT INTO TAR_TEMP_PRICE (PRODUCT_ID, START_DATE, LIST_PRICE, MIN_PRICE, END_DATE)
VALUES (100860, '01-JAN-89', 33.6, 26.88, CURRENT_DATE);
INSERT INTO TAR_TEMP_PRICE (PRODUCT_ID, START_DATE, LIST_PRICE, MIN_PRICE, END_DATE)
VALUES (100861, '01-JUN-90', 50.4, 40.32, CURRENT_DATE);
INSERT INTO TAR_TEMP_PRICE (PRODUCT_ID, START_DATE, LIST_PRICE, MIN_PRICE, END_DATE)
VALUES (100861, '01-JAN-90', 47.04, 37.632, CURRENT_DATE);
INSERT INTO TAR_TEMP_PRICE (PRODUCT_ID, START_DATE, LIST_PRICE, MIN_PRICE, END_DATE)
VALUES (100861, '01-JAN-89', 43.68, 34.944, CURRENT_DATE);
INSERT INTO TAR_TEMP_PRICE (PRODUCT_ID, START_DATE, LIST_PRICE, MIN_PRICE, END_DATE)
VALUES (100870, '01-JAN-90', 3.136, 2.688, CURRENT_DATE);
INSERT INTO TAR_TEMP_PRICE (PRODUCT_ID, START_DATE, LIST_PRICE, MIN_PRICE, END_DATE)
VALUES (100870, '01-JAN-89', 2.688, 2.128, CURRENT_DATE);
由于某种原因,它从上面的代码中添加了部分选择语句。
这是输出:
'ENTER THE PERCENTAGE YOU WISH TO INCREASE THE PRICE BY: '
12
'ENTER THE PRODUCT SEARCH CRITERIA: '
DYN
'YOU HAVE CHOSEN TO UPDATE PRODUCTS WHOSE DESCRIPTION BEGINS WITH DYN WITH A PERCENTAGE INCREASE OF 12 PERCENT.'
'A SCRIPT insert_prices.sql HAS BEEN CREATED AT c:\runscripts\insert_prices.sql'
'THE FOLLOWING PRODUCTS WERE ADDED TO THE INSERT SCRIPT WITH THE ADJUSTED PRICES: '
PRODUCT_ID DESCRIPTION LIST_PRICE MIN_PRICE ADJ_LIST_PRICE ADJ_MIN_PRICE
---------- ------------------------------ -------------- -------------- -------------- --------------
100860 DynaBlaster 400 $35.00 $28.00 $39.20 $31.36
100860 DynaBlaster 400 $32.00 $25.60 $35.84 $28.67
100860 DynaBlaster 400 $30.00 $24.00 $33.60 $26.88
100861 Dynablaster 600 $45.00 $36.00 $50.40 $40.32
100861 Dynablaster 600 $42.00 $33.60 $47.04 $37.63
100861 Dynablaster 600 $39.00 $31.20 $43.68 $34.94
100870 Dynablaster xl60 $2.80 $2.40 $3.14 $2.69
100870 Dynablaster xl60 $2.40 $1.90 $2.69 $2.13
8 rows selected.
SP2-0734: unknown command beginning "CONCAT('IN..." - rest of line ignored.
【问题讨论】:
也许你的脚本有一些控制字符。你从 unix/windows 运行它? 我在 Windows 8 的 SQLPlus 上运行它。 您实际上在INSERT
中形成了 SELECT
,但您的输出在此处显示为列??
该输出用于验证。我添加了脚本的其余部分以显示假脱机后会发生什么。
你能显示文件“insert_prices.sql”的前几行吗?我想也许引号可能会弄乱 SQL 字符串 ...
【参考方案1】:
将此添加到您的 SQL*Plus 提示中。 (SET HEAD OFF
)
SET TERMOUT OFF;
SET VERIFY OFF;
SET SQLBLANKLINES ON;
SET HEAD OFF /* This turns of the headers in result */
SET FEEDBACK OFF /* Turns off the result feedback */
问题是您的SELECT
的结果列具有以CONCAT(..
开头的默认名称作为别名。
原来如此……
CONCAT('INSERTINTOTAR_TEMP_PRICE(PRODUCT_ID,START_DATE,LIST_PRICE,MIN_PRICE,END_DATE)VALUES(',P.PRODUCT_ID||','''||PR.START_DATE||''','||PR.LIST_PRICE
------------------------------------------------------------------------------------------------------------------------------------------------------
当您执行 INSERT
脚本时,甚至会与您的其他插入一起执行。所以,我们必须关闭标题。
【讨论】:
我在阅读您的评论之前修复了它。我将 pagesize 设置为 0 并且它起作用了。您的解决方案也应该有效,因此我将其标记为答案。谢谢!以上是关于如何使用 CONCAT 解决 SQL Plus 中的 SP2-0734?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 sql [重复] 中使用 group_concat() 获取不同的值
如何在sql server中使用group_concat进行查询[重复]
如何在 sql*plus 中使用 dbms_lob.substr