Examples
Complete Scripts, run against BroadSQL; the output shown is the real output.
A complete example
This Script uses most of the scripting features. Save it as customer-report.sql:
-- @description: Customers of one country, up to its highest customer ID
-- @params: country
ON ERROR STOP;
OUTPUT QUIET;
ECHO 'Preparing report for ${country}';
LET max_id =
SELECT MAX(ID)
FROM CUSTOMER
WHERE COUNTRY = ${country};
ECHO 'Maximum customer ID is ${max_id}';
SELECT ID, NAME, COUNTRY
FROM CUSTOMER
WHERE COUNTRY = ${country}
AND ID <= ${max_id};
ECHO 'Report completed';
Run it:
@customer-report.sql country='FR'
BroadSQL> Found 7 queries
BroadSQL> ON ERROR STOP
BroadSQL> OUTPUT QUIET
Preparing report for FR
Maximum customer ID is 3
|-----------|-------------------------|----------|
|ID |NAME |COUNTRY |
|-----------|-------------------------|----------|
|1 |Ann |FR |
|3 |Chloe |FR |
|-----------|-------------------------|----------|
2 rows fetched in 1 ms.
Report completed
What happened:
-- @params: countrymakescountry='FR'mandatory on every call, and the call assigns it before the first statement.ON ERROR STOPends the run at the first failure, so a broken query never produces a partial report.OUTPUT QUIEThides the echo of the following statements, theLETconfirmation and the finalSUCCESSline; the lines printed before it stay. TheECHOmessages and the result table are always shown.LET max_id = SELECT ...stores one value, which is never displayed or exported.${country}and${max_id}are passed to the database as typed values: no quotes around${country}.
Export the same report to a spreadsheet, customers_fr.xlsx, worksheet DATA:
DUMP LIB customer-report.sql country='FR' TO customers_fr AS XLSX;
The Script runs exactly as above, and the file is written only because the run ends SUCCESS.
Calling Scripts from a Script
monthly.bsql sets a variable and passes it to two library Scripts:
LET region = 'EU';
@prepare-data.bsql region=${region};
@create-report.bsql region=${region};
prepare-data.bsql:
-- @params: region
LET prepared_for = ${region};
ECHO 'prepared ${region}';
create-report.bsql:
-- @params: region
ECHO 'report for ${region}, prepared for ${prepared_for}';
Run it with @monthly.bsql:
BroadSQL> Found 3 queries
BroadSQL> LET region = 'EU'
BroadSQL> region = 'EU' (VARCHAR)
BroadSQL> @prepare-data.bsql region=${region}
BroadSQL> Found 2 queries
BroadSQL> LET prepared_for = ${region}
BroadSQL> prepared_for = 'EU' (VARCHAR)
BroadSQL> ECHO 'prepared ${region}'
prepared EU
BroadSQL> @create-report.bsql region=${region}
BroadSQL> ECHO 'report for ${region}, prepared for ${prepared_for}'
report for EU, prepared for EU
BroadSQL> monthly.bsql: SUCCESS (6 statements, 0 failed) [run 3FX975RY]
create-report.bsqlreadsprepared_for, set byprepare-data.bsql: called Scripts share one variable namespace, and an assignment survives the end of the Script that made it.- Only
monthly.bsqlprints a status line. Its counts include the statements of both called Scripts, and the three Scripts share one Run ID. - After the run,
SHOW SCRIPT VARIABLESat the prompt lists bothregionandprepared_for.
Related pages
- Scripting overview
- Output and error handling:
ON ERROR,OUTPUTandECHO. - Nested Scripts and run lifecycle: what the status line means.
- Exporting Script results:
DUMP LIBandDUMP @.
BroadSQL