I have developed following code to call a sql file from shell script testshell.sh
#!/usr/bin/env ksh
feed=`sqlplus -s uname/pwd <<-EOF
#test.sql 'Test_VAl'
/
exit;
EOF`
echo $feed;
My sql file is test.sql which contains following:
Declare
attributeName varchar2(255):=&1;
BEGIN
DBMS_OUTPUT.put_line (attributeName);
END;
I am getting following error while execution.
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
Please tell me how to fix this issue.
If your substitute variable is a string then you need to quote it when it's used, not when it's passed in. At the moment it doesn't have quotes so it's treated as an object identifier, and there is no matching object or variable, hence the error.
So your SQL script would be:
set verify off
DECLARE
attributeName varchar2(255):='&1';
BEGIN
DBMS_OUTPUT.put_line (attributeName);
END;
/
Of course you don't need to define a local variable but I assume you're experimenting with simple cases for now.
The set verify off stops the old and new messages being displayed. Thos are useful for debugging but otherwise are usually just noise.
Then you can call it with:
feed=`sqlplus -s uname/pwd <<-EOF
#test.sql Test_VAl
exit;
EOF`
Or if you include the exit in the script you can do:
feed=`sqlplus -s uname/pwd #test.sql Test_VAl`
Related
The goal is to execute a script hi.sh after an assertion in SQL script.
hi.sh contains:
#!/bin/bash
echo 'Hello World'
I'm trying to execute hi.sh within DO:
DO $$
BEGIN
ASSERT EXISTS (SELECT * FROM pg_catalog.pg_tables WHERE schemaname = 'public'), '"Public" schema is empty';
\! ./hi.sh
END$$;
It should raise error '"Public" schema is empty' and not execute hi.sh but once there are entries in public schema, hi.sh must be executed.
Running the sql script raises error:
psql:assert.sql:5: ERROR: syntax error at or near "\"
LINE 4: \! ./hi.sh
^
However, it will execute shell script once '\!' line is outside DO, like this:
\! ./hi.sh
How can we create a simple SQL script for executing hi.sh based on assertion?
I am facing an issue while executing the following script snippet.
$ORACLEHOME/bin/sqlplus -s $DBUSER/$DBPASSWORD <<EOF
set pages 0 feedback off
SELECT * FROM ERR_STG_ROAMING_PARTNER;
EOF > Err_File.txt
The following error message appears.
./Roaming.sh: line 213: warning: here-document at line 206 delimited by end-of-file (wanted `EOF')
./Roaming.sh: line 214: syntax error: unexpected end of file
Any help will be appreciated.
Try this:
$ORACLEHOME/bin/sqlplus -s $DBUSER/$DBPASSWORD > Err_File.txt <<EOF
set pages 0 feedback off
SELECT * FROM ERR_STG_ROAMING_PARTNER;
EOF
That is, specify the output redirection before the input heredoc redirection. The shell expects EOF to be on its own on the line terminating the heredoc.
The error message you are getting is the shell complaining about finding end-of-file (of the script file) before finding EOF. The usage of EOF for the heredoc delimiter might lead to some confusion here!
I am trying to insert an xml file into oracle table. I got the error as
ORA-19114: error during parsing the XQuery expression:
ORA-06550: line 1, column 13:
PLS-00201: identifier 'SYS.DBMS_XQUERYINT' must be declared
ORA-06550: line 1, column 7:
I figured out that I need to have DBMS_XQUERYINT object in my db. I ran the package available ...product\10.2.0\server\RDBMS\ADMIN. But package is not getting compiled successfully. Any help is appreciated
I want to run a SQL code using shell script and return the message whether the SQL query executed successfully or not. For this I have used unix script given below.
#!/bin/sh
sqlplus -S hr/hr#xe<<EOF
#emp.sql
EOF
var1=$(cat /cygdrive/d/scripts/output.txt | grep -c 'COUNT')
if [ $var1 -ge 1 ];
then
echo "success"
else
echo "failure"
fi
exit;
and emp.sql(called sql file) as
SET ECHO OFF
SPOOL D:\scripts\output.txt
SET LINESIZE 100
SET PAGESIZE 50
SELECT count(*) FROM employees;
SPOOL OFF;
EXIT 0;
When I execute the script I am getting output as
COUNT(*)
----------
107
./script1.sh: line 13: syntax error: unexpected end of file.
I don't know where I should put EOF statement exactly. Also I am not getting the status message whether it is success or failure which I want as output. Please help. Thanks in advance
SPOOL D:\scripts\output.txt Isnt this windows way of referring to a file where as in the shell script you referred to the file as /cygdrive/d/scripts/output.txt. I assume you are using linux shell to execute so I executed your script changing the spool line in sql file. It worked fine.
Edit: Also the \ that you used, for the spooled output.txt path, will cause the sqlplus to terminate. Hence the error line 13: syntax error: unexpected end of file. Perhaps add quotes to the path or use the same file path as you used in shell
I have one .sql file that is execute using ant, when I execute it with the tag I recived a different output as when i used calling "sqlcmd".
sql tag output:
[sql] Executing resource: C:\SqlTesting\TestScriptDependencies\Executor.sql
[sql] Failed to execute: Use Library Exec CallSelectSP
[sql] com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name'Libraty.dbo.libraryDocumentType'.
[sql] 0 of 1 SQL statements executed successfully
exec tag output:
[exec] First SP
[exec] Msg 208, Level 16, State 1, Server MyPC-PC, Procedure getFirstDocumentType, Line 3
[exec] Invalid object name 'Libraty.dbo.libraryDocumentType'.
[exec] Second SP
[exec] Msg 208, Level 16, State 1, Server MyPC-PC, Procedure badSP, Line 3
[exec] Invalid object name 'Libraty.dbo.libraryDocumentType'.
And this is the .sql file.
Print 'First SP'
Exec getFirstDocumentType
Print 'Second SP'
Exec badSP
Go
I wonder if it is a way of the SQL tag reproduce the same output as the EXEC tag.
Thanks.
Looks like the first one is submitting the whole script as a single batch via jdbc. Whereas the second appears to be sending each sql statement via sqlcmd - hence the print statements succeed (and result in synchronized output - which is not always guaranteed with print - raiserror(str, 10, 1) with nowait; is the only guarantee of timely messaging) and both sp calls are attempted, each producing their own (sql) error.