简体   繁体   English

SQL脚本中的变量

[英]Variables in SQL script

I am trying to create a script that accepts different values from a user and inserts them into the database. 我正在尝试创建一个脚本,该脚本接受来自用户的不同值并将其插入数据库中。 For two of the values, HName and SubName, I want to use these to search the database for their corisponding code, and then insert the data into the db. 对于其中两个值HName和SubName,我想使用它们在数据库中搜索其对应的代码,然后将数据插入db。 I got this to work in a different script when I just did it with one variable but I can not get this to work with two. 当我仅使用一个变量执行此操作时,我就可以在另一个脚本中使用它,但是不能使用两个变量进行此操作。 I get the following error: 我收到以下错误:

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:
:= . ( @ % ;

I tried splitting these up into two seperate DECLARE, BEGIN, END blocks but then I still get an error which Im guessing is because I can't access the first variable in the second one. 我尝试将它们分成两个单独的DECLARE,BEGIN,END块,但是我仍然收到一个错误,我猜是因为我无法访问第二个变量中的第一个变量。 Below is my script if somebody can please give any help/advice/tips thank you. 下面是我的脚本,如果有人可以给我任何帮助/建议/技巧,谢谢。

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;
/

I think you can not write sqlplus command in plsql code 我认为您不能在plsql代码中编写sqlplus命令

move the following code out of plsql block: 将以下代码移出plsql块:

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

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

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