Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing sql file from another sql file

I have a .sql file with name Alter_table.sql which have the following code.

alter table mytable add newcolumn VARCHAR2(1);

I don't want to edit this file and add a spool command. However I need to execute Alter_table.sql by writing spool in another file (execute_sql.sql) which should look like the below. I am not sure of the correct syntax. Can anyone please help here?

SET SERVEROUTPUT ON
SET DEFINE OFF
SPOOL Alter_Table_STD_SOURCE.log

EXEC username/password@database `Alter_table.sql`

SPOOL OFF;
SET DEFINE ON
SET SERVEROUTPUT OFF
like image 763
Sarath KS Avatar asked Jun 26 '26 11:06

Sarath KS


1 Answers

(Thanks to Alex Poole) :-)

You need to connect first, then run your .sql file in the wrapper script using the '@' sign or 'START' commands:

...
-- Connect if not already connected.
CONNECT username/password@database

@Alter_table.sql

...

I'm not sure its a good idea to keep login/password in a file but you need to take security into account.

like image 161
Gary_W Avatar answered Jun 28 '26 05:06

Gary_W



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!