with sqlplus, how do i run an explain plan on a query in a file - oracle11g

generally, i can run a .sql file in windows like so:
SQL> #"longpath/sqlfile.sql"
and sqlplus will spit out the results of the query.
but i'm trying to run something like:
SQL> EXPLAIN PLAN FOR #"longpath/sqlfile.sql";
i'm getting an error
EXPLAIN PLAN FOR #"longpath/sqlfile.sql"
*
ERROR at line 1:
ORA-00905: missing keyword
not sure what keyword im missing...

Related

How can I quit sqlite from a command batch file?

I am trying to create a sealed command for my build pipeline which inserts data and quits.
So far I have created my data files
things-to-import-001.sql and 002 etc, which contains all the INSERT statements I'd like to run, with a file per table.
I have created a command file to run them
-- import-all.sql
.read ./things-to-import-001.sql
.read ./things-to-import-002.sql
.quit
However when I run my command
sqlite3 -init ./import-all.sql ./database.sqlite
..the data is inserted, but the program remains running and shows the sqlite> prompt, despite the .quit command. I have also tried using .exit 0.
From the sqlite3 --help
-init FILENAME read/process named file
Docs: https://www.sqlite.org/cli.html#reading_sql_from_a_file
How can I tell sqlite to exit once my inserts have finished?
I have managed to find a dirty workaround for this issue.
I have updated my import file to include a bad command, and executed using -bail to quit on first error.
-- import-all.sql
.read ./things-to-import-001.sql
.read ./things-to-import-002.sql
.fakeErrorToQuitWithBail
Then you can execute with
sqlite3 -init import-all.sql -bail
and it should quit with
Error: unknown command or invalid arguments: "fakeErrorToQuitWithBail". Enter ".help" for help
Try using ".exit" at the place of ".quit". For some reason SQLite dont doccumented this commands.
https://www.tutorialspoint.com/sqlite/sqlite_commands.htm

Why am I getting: database is locked, in an SQLite3 script?

I'm getting an error when running an SQLite script.
--drop use table before replacing it
DROP TABLE IF EXISTS db.use;
--Create the use table in the saved database
CREATE TABLE db.use AS SELECT * FROM use2; -- this is the line that generates the error: Error: near line 145: database is locked
Are these two statements run asynchronously or something? I don't understand what's causing the error, but I'm wondering if it has to do with that.
Might there be a way to run the script in a lock-step manner, i.e. non-asynchronously?
This is how I was running the command: sqlite3 --init script_name.sql dbname.db, and elsewhere in the script I had an ATTACH statement reading the same database dbname.db. Essentially reading the same file twice.
The way I solved this was by executing the script in the sqlite3 shell:
sqlite3> .read script_name.sql
Have you tried to add a commit statement after the drop statement?
I think that would make sure the create table statement run after the drop statement is totally done.

how to include new line in scipt log which is calling sql file and that sql file is giving lot of errors

I have written a shell script which is calling another sql file, which is disabling triggers in oracle.
but this sql file is generating lot of oracle errors and in log file of script, I am getting response like
ORA-21021 -- oracle errros ORA-20111 -- oracle errors
I would like to add a new line getting the oracle errors something like
ORA-21021 --** oracle errors**
ORA-20111 --** oracle errors**
How this can be done?
sql file code:
alter trigger trigger_name1 disable;
alter trigger trigger_name2 disable;
alter trigger trigger_name3 disable;
alter trigger trigger_name4 disable;
alter trigger trigger_name4 disable;
A solution with perl
perl -pe 's/ORA-\d/\n$&/g' <oldfile >newfile
to add a newline before sequences ORA- followed by a number (\d)
I was able to achieve this by below method:
SQL=$(echo "$SQL" | sed -e 's/ERROR/\\n\rERROR/g')
echo $SQL

Grant UTL_HTTP permission in PLSQL

I would like to get HTML content from a certain webpage in my function. I read I can do it with the UTL_HTML package in PLSQL. So I made the following code in the project:
v_webcontent := utl_http.request(v_weblink);
Here the v_webconent and v_weblink are declared earlier. running this in de function gives an PLSQL exception: PLS-00201: identifier 'UTL_HTTP' must be declaredI guess this problem is because the package isn't available (from this link: same error message).
I followed the advice. So I created a new database connection in sql developer as the SYSTEM role (SYS didn't work, it sayd I could only logon using SYSDBA or SYSOPER but both wouldn't take the standard password I created with the database). Then I entered the code in the link above.
GRANT EXECUTE ON SYS.UTL_HTTP TO [database];
The user I created is named 'Database'. It first gave me an error without the [] square brackets. Table or view does not exist so I then put the brackets around it. Now it gives error:
Error starting at line : 1 in command -
GRANT EXECUTE ON SYS.UTL_HTTP TO [database]
Error report -
SQL Error: ORA-00987: missing or invalid username(s)
00987. 00000 - "missing or invalid username(s)"
*Cause:
*Action:
So I have no idea how to fix this. In the link above OP said that he got an other error, so I also checked if I didn't have the same problem. I entered:
SELECT * FROM dba_objects WHERE object_name='UTL_HTTP'
It returned 4 entry's. With owners: SYS, SYS, PUBLIC and APEX_040000.
Can somebody help me? Do I need to logon as SYS and with what passwords?
Since 11g you also have to create an access control list ('ACL') which specifies which users have access to particular domains, it is no longer sufficient to just grant users execute privileges on utl_http!
Something like this should work in 11g(after granting execute privileges on UTL_HTTP to your database user, as specified by the accepted answer) :
SQL> BEGIN
2 DBMS_NETWORK_ACL_ADMIN.CREATE_ACL(acl=>'mikesacl.xml',
3 description=>'access control list example',
4 principal=>'HR',
5 is_grant=>TRUE,
6 privilege=>'connect');
7 commit;
8 end;
9 /
PL/SQL procedure successfully completed.
SQL> begin
2 DBMS_NETWORK_ACL_ADMIN.ASSIGN_ACL(
3 acl=>'mikesacl.xml',host=>'*');
4 commit;
5 end;
6 /
PL/SQL procedure successfully completed.
Here is a very helpful link which explains the parameters of the above two functions :
http://www.oracleflash.com/36/Oracle-11g-Access-Control-List-for-External-Network-Services.html
Good luck!
Log on as SYS AS SYSDBA.
Execute grant execute on sys.utl_http to "Database"; Do not use any square brackets!
That should work.
Piece of advice: Do not name your DB user 'Database'.
To reset your SYS password
Run cmd.exe as administrator.
cd to your ${ORACLE_HOME}/database.
Find the PWDsomething.ora file there (where something will be your instance name), copy its name (into clipboard).
Run orapwd file=PWDsomething.ora password=SomePasswordOfMine force=y, where PWDsomething.ora will be replaced with the file name from the step 3 and SomePasswordOfMine must be replaced by whatever password you wish to have.
That might work.

Oracle Exception Detail

I'm using Flyway 3.0 within ANT and I would like to know if the stack when an Oracle error occur could be more detailed.
Example: if my migration script contain this statement:
DROP TABLE FOO$;
And this table doesn't exist, I expect:
ORA-00942 : table or view does not exist
But I got:
Flyway Error: org.flywaydb.core.internal.dbsupport.FlywaySqlScriptException: Error executing statement at line 4: DROP TABLE FOO$
Not optimal for root cause analysis...
Any idea for better verbosity ?
You can use Ant's standard -d switch to reveal more info. If you feel the standard info should be improved, please file a bug report in the issue tracker.
Adding the -X option to the flyway command line will print debug output.

Resources