2014-05-21 26 views
0

shell脚本,SQL文件,我已经开发了下面的代码从shell脚本调用SQL文件testshell.sh调用从参数

#!/usr/bin/env ksh 
feed=`sqlplus -s uname/pwd <<-EOF 
@test.sql 'Test_VAl' 
/
exit; 
EOF` 
echo $feed; 

我的SQL文件是test.sql其中包含以下内容:

Declare 

attributeName varchar2(255):=&1; 
BEGIN 
    DBMS_OUTPUT.put_line (attributeName); 

END; 

我在执行时遇到错误。

old 3: attributeName varchar2(255):=&1; 
new 3: attributeName varchar2(255):=Test_VAl; 
attributeName varchar2(255):=Test_VAl; test.sql testshell.sh 
ERROR at line 3: 
ORA-06550: line 3, column 31: PLS-00201: identifier 'TEST_VAL' must be declared 
ORA-06550: line 3, column 16: PL/SQL: Item ignored 
ORA-06550: line 5, column 25: PLS-00320: the declaration of the type of this expression is incomplete or malformed 
ORA-06550: line 5, column 3: PL/SQL: Statement ignored 

请告诉我如何解决此问题。

回答

2

如果您的替代变量是一个字符串,那么您需要在使用它时引用它,而不是在传入时引用它。此刻它没有引号,因此它被视为对象标识符,并且没有匹配的对象或变量,因此错误。

所以,你的SQL脚本是:

set verify off 
DECLARE 
    attributeName varchar2(255):='&1'; 
BEGIN 
    DBMS_OUTPUT.put_line (attributeName); 
END; 
/

当然,你并不需要定义一个局部变量,但我假设你用简单的情况下经历过了,现在。

set verify off停止显示oldnew消息。这对于调试很有用,否则通常只是噪声。

然后你可以把它叫做:

feed=`sqlplus -s uname/pwd <<-EOF 
@test.sql Test_VAl 
exit; 
EOF` 

或者如果包括exit的脚本,你可以这样做:

feed=`sqlplus -s uname/pwd @test.sql Test_VAl` 
+0

感谢亚历克斯。 这是问题所在。我在sqldeveloper中尝试了相同的代码,它工作正常,这让我相信sql部分是正确的。 –