Help Import, export and backup

Running an SQL script

Execute SQL Script runs every statement in a file against the current connection, without loading the file into the query editor. Use it to restore an SQL dump, or to run a long script that would be slow or awkward in the editor.

The Execute Query(s) From A File dialog

Opening it

The dialog is called Execute Query(s) From A File.

Running a file

  1. Check Current database. Statements run there unless the file has its own USE statements. A dump made with Include “USE database” statement switches database by itself.
  2. Enter the file in File to execute, or click … to choose it. The file is remembered for next time.
  3. Leave Abort on error ticked to stop at the first statement that fails, or untick it to carry on past errors.
  4. Click Execute.

If a database is selected in the Object Browser, bonSQL first asks you to confirm:

The current database context is shop. SQL statements in the file will be executed here, unless one or more USE statement(s) specifying another database context is in the file. Do you want to continue?

While it runs, the bar shows how much of the file has been read and the button reads Stop. At the end, the dialog shows how many statements were run and one of these:

MessageMeans
Import successfulEvery statement ran.
Import successful with error(s)The file was run to the end, but some statements failed (only with Abort on error unticked).
Import unsuccessfulA statement failed and Abort on error stopped the run.
Aborted by userYou clicked Stop.
Import unsuccessful. Error opening batch fileThe file could not be opened.

When the file has been run to the end, Close changes to Done, and bonSQL refreshes the database in the Object Browser so that new tables show up. After a stopped or aborted run, refresh the Object Browser yourself (Edit > Refresh Object Browser, F5) to see what was created.

The script runs on a connection of its own, so session settings made by the script (such as SET statements) do not carry over to your query tabs.

The error file

Every statement that fails is written to bonsql.err in %APPDATA%\bonSQL (or in the folder given with -dir), with the statement itself, the time, the line number in the script, the error code and the server’s message. New errors are added to the end of the file; it is never emptied for you.

When Abort on error stops a run, the SQL Dump Import Error dialog says where the file is. Click Open Error File… to open it in Notepad.

Scripts with stored procedures

bonSQL understands DELIMITER lines, as written by a dump or by hand around procedures and triggers. Comment lines starting with # or -- inside a procedure, function or trigger body are kept and stored with it on the server; comment lines between statements are skipped.

Transactions

When Enable transaction support for batch process is ticked on the General tab of Tools > Preferences, bonSQL runs the script inside a transaction (set autocommit=0) and commits it when the script has run to the end. If the run stops early, through Abort on error or Stop, bonSQL does not commit, and the uncommitted changes are lost when its connection closes. Statements that the server commits by themselves, such as CREATE TABLE, are not undone.

Tips

When it does not work