On this page
@
Runs a Script: a text file of SQL statements and BroadSQL commands.
Syntax
@<script> [<name>=<value> ...]
Quick start
@maintenance/cleanup.bsql
Runs the Script maintenance/cleanup.bsql of the Scripts Library.
@reports/customer.bsql customer_id=42
Runs a Script that reads ${customer_id}.
Arguments
| Argument | Required | Values | Default |
|---|---|---|---|
<script> | Yes | A path in the Scripts Library, a ./ path, or an absolute path; in double quotes when it contains spaces | None |
<name>=<value> | No, unless the Script requires it | A named argument: a number, a quoted string, TRUE, FALSE, NULL or ${variable} | For a -- @param parameter: the variable of that name set by LET, otherwise its default |
Examples
@maintenance/cleanup.bsql
@./helper.bsql
@reports/customer.bsql customer_id=42 country='FR'
@"C:\My Scripts\foo.bsql" since=${last_run}
Example session
@countries/largest.sql continent='Europe';
-- continent = 'Europe'
-- min_population = 50000000
|----|----------------------------------------------------|-----------|
|CODE|NAME |POPULATION |
|----|----------------------------------------------------|-----------|
|RUS |Russian Federation |146934000 |
|DEU |Germany |82164700 |
|GBR |United Kingdom |59623400 |
|FRA |France |59225700 |
|ITA |Italy |57680000 |
|UKR |Ukraine |50456000 |
|----|----------------------------------------------------|-----------|
6 rows fetched in 12 ms.
countries/largest.sql: SUCCESS (1 statement, 0 failed) [run BUZ2SI4X]
Details
A Script is a text file of SQL statements, BroadSQL commands, or both, separated by ;. The file extension means nothing to BroadSQL. A reference is found the same way by every command and by the Editor.
Where the Script is found
@foo.bsqland@maintenance/foo.bsql: relative to the Scripts Library root, never to the working directory. No extension is ever guessed:@foois the file namedfoo.@./foo.bsql: relative to the working directory when typed at the prompt, and to the folder of the running Script when written inside a Script, so a folder of Scripts can be moved as a whole.@C:\temp\foo.bsqlor@/tmp/foo.bsql: an explicit path to a Script outside the library.
Put a reference that contains spaces in double quotes.
Named arguments
@report.bsql customer_id=42 country='FR': each argument sets a scripting variable for this call, and the Script reads it as ${customer_id}.
- A value is a number, a single-quoted string,
TRUE,FALSE,NULLor one${other}variable. - Every value is computed before any is set, and a call that fails sets none of them. There is no limit on the number of arguments.
- The arguments last as long as the call: when the Script returns, a variable of the same name gets back the value it had before, or is undefined again. A
LETrun by the Script itself stays. - A parameter declared with
-- @paramand not passed takes the value of the variable of the same name set byLET, otherwise its default:LET continent = 'Europe';then@largest.sql;usesEurope. A required parameter with neither refuses the call. - A Script can require arguments with a
-- @params: customer_id, countrycomment line: each call must then pass them explicitly.
The positional parameters %1 to %9 were removed: @report.bsql 42 FR fails with an explanation. Write @report.bsql customer_id=42 country='FR' and use ${customer_id} in the Script.
Result
At the end of a Script started at the prompt, a status line gives the result (SUCCESS, COMPLETED_WITH_ERRORS, FAILED or CANCELLED), the number of statements run and failed, and a run identifier. @, LIB RUN and the BroadSQL Editor run Scripts the same way.
Related
- LIB RUN
- LET, scripting variables
- ON ERROR
- Running Scripts
- Script arguments
- Scripting overview
Used in these how-tos
- How do I find orphan rows, duplicates and missing values?
- How do I run a post-deployment smoke check on every environment?
- How do I save a query with parameters and run it by name?
- How do I write a report Script that stops at the first failure?
- Largest countries of a continent Scripts Gallery
- Countries by ISO code Scripts Gallery
- Largest cities of a list of countries Scripts Gallery
- Estimated row count of every table (PostgreSQL) Scripts Gallery
- Largest tables on disk (PostgreSQL) Scripts Gallery
- Blocked and blocking sessions (PostgreSQL) Scripts Gallery
- Long-running queries (PostgreSQL) Scripts Gallery
- Largest tables on disk (SQL Server) Scripts Gallery
- Blocked requests (SQL Server) Scripts Gallery
- Duplicate values in a column (template, runs on WORLD) Scripts Gallery
- Rows changed since a time (template, runs on WORLD) Scripts Gallery
- Rows missing between two databases (template, WORLD and WORLD-QA) Scripts Gallery
Last modified in release 5.5.0.
Found something wrong or unclear on this page? Tell us, with the page address.