How to import external sql scripts from a sql script in PostgreSQL? - sql

I was wondering if it is possible/how to include one .sql file in another .sql file, as is done in other programming languages like C or Java?
I am asking because I wanted to organize my sql scripts into support library files and application scripts, and so on. I searched around, and solutions seem to be about loading .sql files from the psql client (see e.g. postgreSQL - psql \i : how to execute script in a given path). What I am interested in is to load a library .sql script from inside a sql script.
P.S. I am using PL/pgSQL (PostgreSQL 9.3).

Based on the answer It is possible to reference another SQL file from SQL script, on PostgreSQL, you can include another SQL's files just using the \i syntax. I just tested and is working good on PostgreSQL 9.6:
\i other_script.sql
SELECT * FROM table_1;
SELECT * FROM table_2;

Related

Is there an alternative way to import data into Postgres than using psql?

I am under strict corporate environment and don't have access to Postgres' psql. Therefore I can't do what's shown e.g. in the SO Convert SQLITE SQL dump file to POSTGRESQL. However, I can generate the sqlite dump file .sql. The resulting dump.sql file is 1.3gb big.
What would be the best way to import this data into Postgres? I also have DBeaver and can connect to both databases simultaneously but unfortunately can't do INSERT from SELECT.
I think the term for that is 'absurd', not 'strict'.
DBeaver has an 'execute script' feature. But who knows, maybe it will be blocked.
EnterpriseDB offers binary downloads. If you unzip those to a local drive you might be able to execute psql from the bin subdirectory.
If you can install psycopg2 or pg8000 for python, you should be able to connect to the database and then loop over the dump file sending each line to the database with cur.execute(line) . It might take some fiddling if the dump file has any multi-line commands, but the example you linked to doesn't show any of those.

MonetDB - How to call .SQL files from within SQL files?

How can I run .SQL files from other .SQL files using MonetDB?
I went through the whole documentation but had no success finding it.
For instance, in MySql I could do something like
parent.sql
use mydatabase;
source child1.sql
source child2.sql
child1.sql
SELECT * from Products;
child2.sql
SELECT * from Orders;
How can I do that (or something similar) using MonetDB?
To dump the SQL database, start the MonetDB SQL Client program and type the command
\>...\databasedump.sql
\D
\>
The path after > should be an absolute path name (i.e. start with a drive letter) and be in a save location. By default the database is located in %APPDATA%\MonetDB5. After having made a database dump it can be removed. This folder is located inside the dbfarm\demofolder.
Restoring the SQL database can be done using the MonetDB SQL Client program with the following command
\<...\databasedump.sql
Source:
https://www.monetdb.org/Documentation/UserGuide/DumpRestore

choosing where to save sqlite database

This is probably a simple question, but I could use some help. I am trying to build a small database for an application that will only be run on my computer so I want to create a local database.
To do this I am trying to use sqlite. I can use the command prompt to make what seems to be a database by using the sqlite3 databaseName; functionality, but I do not know where it is being stored.
I need to be able to find the database to access it through the application I am experimenting with. I already know all of the basic sql and such for creating the database tables and data, but I cannot figure out how to simply make the database connection.
is there a way to specify where the database .db file will be stored, and why can I not find the file it seems to be making?
Using sqlite3 shell? Some help using sqlite3 -help:
Usage: sqlite3 [OPTIONS] FILENAME [SQL]
If FILENAME is not supplied, shell uses an temporary database.
If you start shell without supplying a filename, you may save temporary database at any time using:
sqlite> .backup MAIN "folder\your_file.extension"
Or you can ATTACH an existing database an use SQL methods:
sqlite> ATTACH DATABASE "path\stored.db" AS other;
sqlite> INSERT OR REPLACE INTO other.table1 SELECT * FROM this_table1;
sqlite> DETACH other;
For doing such things , you can use Sqlite Manager , you'll get it as a Firefox addon. It's excellent in creating / Managing Sqlite database.
https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/
Thanks everyone for answering, but it turns out my issue was much simpler than I thought.
I was trying to name the database after already starting the shell.
I was supposed to create the database from command line by doing sqlite3 name.db
But I was trying to use that command within the sqlite shell so nothing was being created.

How can I read a .dump file with R ?

Suppose I have a .dump file that I would like to convert to a data.frame that I can then analyse in R. I know that sqldf allows us to use SQL commands on data frames but can we also use this or another package to read in .DUMP files?
This post shows how to restore a .dump file with SQL but this syntax doesn't work in sqldf. Of course, I have no idea how to properly use SQL syntax, so it is likely my fault.
This is what I think the syntax should look like based on the post mentioned:
mysql create database mydb;
mysql use mydb;
mysql source dumpname.dump;

Mysql: How to call sql script file from other sql script file?

Suppose I have wrote script Table_ABC.sql which creates table ABC. I have created many such scripts for each of required tables. Now i want to write a script that call all of these script files in a sequence so basically I want another script file createTables.sql. Mysql provides option to execute a script file from "mysql" shell application but could find some command like exec c:/myscripts/mytable.sql. Please tell me if there is any command that can be written in sql script itself to call other one in latest mysql versions or alternative for same.
Thanks
You can use source command. So your script will be something like:
use your_db;
source script/s1.sql;
source script/s2.sql;
-- so on, so forth