SQL脚本中的变量

时间:2014-04-21 13:42:33

标签: database oracle variables relational-database

我正在尝试创建一个接受用户不同值的脚本,并将它们插入到数据库中。对于其中两个值HName和SubName,我想使用它们在数据库中搜索其相应的代码,然后将数据插入到数据库中。当我用一个变量完成它时,我得到了这个在不同的脚本中工作,但我不能让它与两个一起工作。我收到以下错误:

Enter a line number:  1
Enter a description for the item:  5 lbs
Enter an amount for the item:  30.49
Enter a Head category for the item:  Grocery
old  13:        WHERE Name = '&HName';
new  13:        WHERE Name = 'Grocery';
old  22:        WHERE Name = '&SubName'
new  22:        WHERE Name = 'Meat'
old  28:                       VALUES (&ExpNum, &LineNum, '&Descr', &Amt, HCode, SCode);
new  28:                       VALUES (         1,          1, '5 lbs', 30.49, HCode, SCode);
    ACCEPT SubName PROMPT 'Enter a Sub Head category for the item:  ';
           *
ERROR at line 17:
ORA-06550: line 17, column 9:
PLS-00103: Encountered the symbol "SUBNAME" when expecting one of the
following:
:= . ( @ % ;

我尝试将它们分成两个单独的DECLARE,BEGIN,END块然后我仍然得到一个错误,我猜是因为我无法访问第二个中的第一个变量。以下是我的脚本,如果有人可以请提供任何帮助/建议/提示,谢谢。

ACCEPT ExpNum NUMBER PROMPT 'Enter an expense number:  ';

SELECT *
FROM EXPDET
WHERE ExpNum = &ExpNum;


ACCEPT LineNum NUMBER PROMPT 'Enter a line number:  ';
ACCEPT Descr PROMPT 'Enter a description for the item:  ';
ACCEPT Amt PROMPT 'Enter an amount for the item:  ';
ACCEPT HName PROMPT 'Enter a Head category for the item:  ';

DECLARE
    HeadCode NUMBER;
    HCode NUMBER;

    SubCode NUMBER;
    SCode NUMBER;


BEGIN
    SELECT Code
    INTO HeadCode
    FROM HEAD
    WHERE Name = '&HName';

    HCode := HeadCode;


    ACCEPT SubName PROMPT 'Enter a Sub Head category for the item:  ';

    SELECT SubCode
    INTO SubCode
    FROM SUBHEAD
    WHERE Name = '&SubName'
    AND HCode = HCode;

    SCode := SubCode;

    INSERT INTO EXPDET
    VALUES (&ExpNum, &LineNum, '&Descr', &Amt, HCode, SCode);

END;
/

1 个答案:

答案 0 :(得分:0)

我认为你不能在plsql代码中编写sqlplus命令

将以下代码移出plsql块:

ACCEPT SubName PROMPT 'Enter a Sub Head category for the item:  ';