Usually, when we run SQL scripts, it is done on an ad hoc basis. We usually run one file at a time just once. From time to time there is the need to run a set of SQL script files. One common reason I’ve seen for doing this is to rebuild a database for testing. If you had all of your create tables/stored procedure commands stored in a series of SQL script files, then you could rebuild your database fairly easily by executing the SQL files in sequence.

Vbscript

I have written plenty of VBA in Access applications. For this, I am just trying to set up a SQL script as a VBS script, so the users don't have to go into SSMS to run it. They can just double-click the VBS script, specify the server and database when prompted, and the quick script will run for them. How to Run VBScript from SQL Server 2008. SQL developers can run vb script files from SQL Server t-sql codes by using xpcmdshell procedure. It is sometimes a requirement especially for web developers to call vb script from SQL Server engine and execute vbscript from sql code. Of course calling vbscript from sql can be solved by using CLR in. Can I install File Sight v7.1.1.54 on SQL 2016? You give us an option to use SQLite or others database in File Sight. What the schema of the database? Powershell Execute Script Action. Using SQL server. How to Monitor SQL Server (Link is broke is your webpage) powershell action script not firing when tested against certain servers. Oct 13, 2014 The sqlcmd utility lets you run entire SQL script files full of t-sql via command prompt. Leveraging this tool, you can create a batch file to run the sql script files in sequence. How To Execute A SQL Script File Via Command Prompt With SQLCMD. Although there are TONS of command line options for executing a sql script. Just executing a sql.

The cat Command. The following code demonstrates the first option for running an SQL script from.


SQL Server provides a tool to do this called SQLCMD.

The sqlcmd utility lets you run entire SQL script files full of t-sql via command prompt. Leveraging this tool, you can create a batch file to run the sql script files in sequence.

.sql


How To Execute A SQL Script File Via Command Prompt With SQLCMD

Although there are TONS of command line options for executing a sql script. Just executing a sql script file with the basic options is very easy.

The above command uses the -i option to specify the input file of DropTables.sql. Everything inside this file will be execute on the current database.


NOTE: When you create your SQL script files, I always recommend putting a USE statement at the top of your script. This will ensure that you are using the correct database when your dropping and creating tables.


Run A Batch Of SQL Script Files

Now that you know how to run one SQL script file… running multiple should be easy. All you need to do is create a batch file and run one script after the other.


Your batch file will look something like this:

2
4
SQLCMD/i'c:scriptsCreateTables.sql'
PAUSE


What About Errors?

So what happens if the SQL script file throws an error? The above commands I showed you will just ignore any errors. If you want to capture your errors, you need to add the command line option /b. This flag will tell the sqlcmd utility to stop processing the script and output a DOS ERRORLEVEL value. You can then check the DOS ERRORLEVEL value after each execution.

2
4
6
8
IF%ERRORLEVEL%>(PAUSE)
SQLCMD/i'c:scriptsCreateTables.sql'/b
IF%ERRORLEVEL%>0(PAUSE)


Reference: http://msdn.microsoft.com/en-us/library/ms162773.aspx

Introduction to Running an SQL File in Postgres

This tutorial will explain how to run an SQL file in Postgres, using the .sql extension, in a terminal or command prompt window. This can be accomplished in one of two ways, from inside of the PSQL command-line interface itself or from outside of PSQL. The article will also provide an overview of the options for the commands used to run a SQL file in Postgres.

Prerequisites to Run an SQL file in Postgres

  • PostgreSQL must be properly installed and running. Execute the service postgresql status command to confirm the status of PostgreSQL is active. Press the CTRL + C keys to exit.

  • The interactive PSQL command-line for PostgreSQL must be properly installed and working. Execute the psql -V command to confirm the status. The results should resemble the following:

Create a SQL File that will Execute with PostgreSQL

Navigate to the project folder or directory by executing the cd (change directory) command in a terminal or command-prompt window.

Create an SQL file using a text editor or terminal-based editor

Create the SQL file with a text editor or a terminal-based editor like nano, gedit or vim. Execute the following bash command to enter the .sql file in the current directory using nano:

nano test.sql

Once inside, enter some SQL commands into the SQL file for later execution. Following is an example of a command used to return all of the record rows in the PostgreSQL table named some_table:

The results should resemble the following:

Execute sql script fileVbscript to run .sql file l file in psql

NOTE: Just press CTRL+O to output any changes to the file in nano. However, be aware this will overwrite any changes to the file, or it will create a new file called test.sql if a file does not already exist.

Run a SQL file in Postgres using the ‘psql’ command

Now execute the SQL file in the terminal window. Confirm there is a database created with some table data that can be used to test the psql command. A role or user for that database will also be required.

Options for the ‘psql’ command to run a SQL file in Postgres

Following is a brief explanation of each of the options, or flags, used for the psql command that will execute the SQL script file:

psql -U USER_NAME_HERE — The -U flag is used to specify the user role that will execute the script. This option can be omited if this option’s username is the first parameter. The default username is the system’s current username, if one has not been explicitly specified.

psql -h 127.0.0.1 — The -h flag is for the remote host or domain IP address where the PostgreSQL server is running. Use 127.0.0.1 for a localhost server.

psql -d some_database — The -d option is used for the database name.

psql -a — The -a or --echo-all flags will print all of the lines in the SQL file that conatin any content.

psql -f /some/path/my_script_name.sql — The -f option will instruct psql to execute the file. This is arguably the most critical of all the options.

Execute the SQL file in PostgreSQL from the terminal

The final step in this process is to execute the file using the psql command while connecting with an username, host and database. Note that this example uses the objectrocket role.

The following command can be used to access the test.sql file created earlier:

psql objectrocket -h 127.0.0.1 -d some_database -f test.sql

Use the ‘-a’ option to print everything while executing a SQL file in Postgres

The -a option, also referred to as the --echo-all option, is used to echo, or print, all of the input lines that contain any content. This includes the SQL comments and the original SQL commands or statements being executed.

The following is an example of the --echo-all option:

psql objectrocket -h 127.0.0.1 -d some_database -f-a test.sql

The results should resemble the following:

NOTE: Be aware that the -a option will print everything contained in the file, including commands and the original SQL statement.

Connect to PostgreSQL and then run a SQL file using ‘psql’

The i command can aslo be used to execute the SQL file from within the psql interface.

Thepsql must be entered though a database and a specified user or by connecting to a database once inside psql, using a ROLE that has access to the database, with the c command.

Connect to PostgreSQL without a database connection

The following example demonstrates how to connect to PostgreSQL with a specified ROLE, but without specifying a particular database:

The following example uses the c command, followed by the database name, to connect to a database before executing the SQL script:

c some_database

Execute the following i command to run the SQL file:

NOTE: If no path is specified, then PSQL will use the directory that was last accessed before connecting to PostgreSQL in order to execute the SQL file.

The following screenshot provides an overview:

Vbscript To Run .sql File Converter

Conclusion to Executing a SQL File in Postgres

Vbscript To Run .sql File Extension

This tutorial explained two ways to run an SQL file in Postgres, specifically from inside the PSQL command-line interface or from outside of PSQL using the -f option. The article covered how to create an SQL file using a text editor or terminal-based editor that will execute with PostgreSQL, how to execute the SQL file from the terminal, connect to PostgreSQL and run a SQL file. The tutorial also covered how to run a SQL file in Postgres using the PSQL’ command and the options for the commands along with an explanation of each option. Remember that if no path is specified when connecting to PostgreSQL without a database connection, PSQL will use the directory that was last accessed prior connecting to PostgreSQL to execute the SQL file.