On this page
How do I load a CSV file prepared in Excel into a table, with a preview first?
What you'll accomplish
Someone prepares rows in a spreadsheet and asks you to put them in a table: new reference values, a list of planned operations, corrections. Inserting them by hand is slow, and loading a file blindly risks a half-loaded table or values in the wrong columns.
You will save the sheet as a CSV file, check it against the target table with a preview that writes nothing, then insert every row in one transaction. The preview tells you, before anything changes, which columns match, how many rows are valid and which values would be rejected.
Short answer
Save the sheet as UTF-8 CSV with a header row, run LOAD <table> <file> PREVIEW;, then the same command without PREVIEW to insert after a confirmation.
Before you start
LOAD inserts into an existing table, so the example creates a small practice table of its own in WORLD: MY_VISIT, a list of planned visits to cities of the CITY table. Creating it changes the sample database, and Clean up removes it. (If you made WORLD the production connection of the practice set, run the example on WORLD-QA instead, or BroadSQL asks for a confirmation before each change.) Connected to WORLD:
CREATE TABLE MY_VISIT (CITY_ID INTEGER NOT NULL, VISIT_DATE DATE NOT NULL, NOTE VARCHAR(100));Example
In Excel, prepare three rows under the headers CITY_ID, VISIT_DATE and NOTE (2974 is Paris, 3068 Berlin and 1464 Roma in CITY), then save the sheet as "CSV UTF-8" under the name my_visits.csv in the export folder (the DefaultFolder setting, c:\temp\ by default on Windows). Opened in a text editor, the file looks like this:
my_visits.csvCITY_ID;VISIT_DATE;NOTE
2974;2026-11-03;Kick-off meeting
3068;2026-11-17;"Workshop; day 1"
1464;2026-12-01;NULLThe separator is a semicolon, the CsvSeparator setting; the quotes keep the ; of Workshop; day 1 inside one value, and the word NULL stands for no value. Now check the file against the table, without writing:
LOAD MY_VISIT my_visits.csv PREVIEW;The preview names the target, the file and the counts, then how each header matches a column:
WORLD> LOAD MY_VISIT my_visits.csv PREVIEW;
LOAD PREVIEW
------------------------------------------------------------
Target : WORLD / PUBLIC.MY_VISIT
Mode : INSERT
Source : c:\temp\my_visits.csv
Input rows : 3
Valid rows : 3
Rejected : 0
Columns:
CITY_ID -> CITY_ID INTEGER
VISIT_DATE -> VISIT_DATE DATE
NOTE -> NOTE VARCHAR
No changes have been made.Nothing was rejected, so insert the rows. Without PREVIEW, LOAD reads and checks the file again, then asks Load N rows into TABLE? [y/N]; answer y:
LOAD MY_VISIT my_visits.csv;BroadSQL inserts the three rows and commits once, at the end:
3 row(s) inserted into PUBLIC.MY_VISIT (source rows read: 3, committed).The visits can now be joined with the sample's own data:
SELECT V.VISIT_DATE, TRIM(C.NAME) AS CITY, C.COUNTRYCODE, V.NOTE
FROM MY_VISIT V JOIN CITY C ON C.ID = V.CITY_ID ORDER BY V.VISIT_DATE;When the preview rejects rows
Change 2026-11-17 into 17 November in the file and run the preview again: Valid rows drops to 2, Rejected becomes 1, and a line names the row, the column, the value and the reason. One rejected row prevents the whole load, so correct the file and preview again until nothing is rejected.
Variations
- Without the question:
LOAD MY_VISIT my_visits.csv EXECUTE;authorizes the insert explicitly. In a Script, plainLOADrefuses to write, so usePREVIEWorEXECUTEthere. - Another folder: give a path,
LOAD MY_VISIT c:\data\my_visits.csv PREVIEW;. - A comma separated file: set
CsvSeparator=COMMAinBroadSQL.ini; the default isSEMICOLON.
Watch out
LOADinserts only: it never updates existing rows. Loading the same file again inserts the same rows again; there is no automatic deduplication.- Columns are matched by header name, not position. An unknown header refuses the whole load.
- The preview checks types, not constraints: a duplicate key is reported by the database when the rows are inserted, and the import is rolled back.
- Finish other pending work first:
LOADcommits or rolls back the connection's transaction.
Clean up
Remove the practice table when you no longer need it:
DROP TABLE MY_VISIT;Related
Commands used
Found something wrong or unclear on this page? Tell us, with the page address.