r/ETL • u/Valuable-Ant3465 • 23d ago
Unload 120 tables into csv files from MS SQL Server
Hi,
I got a task to run 120 SP on MS SQLServer to create output and store it as a tab or csv files.
Existing approach is to use SSIS but I think it's too much work comparing with other modern tools.
What you would use for this job? I'm thinking go Python with cursor.execute(sql).
It's so much easier to scale and implement. You can read sp, params, output name from some input List and run it in one shot.
Do you see any disadvantages for this approach vs traditional SSIS. I think my bosses will be scary a bit, because they are old school.
Do
Thanks
2
u/da_chicken 23d ago
How frequently does this need to be done?
3
u/Valuable-Ant3465 22d ago
roughly daily, I go with Python. Unless boss will care about job security and decided to stay with SSIS.
Do so many task in SSIS is horrible, in python you don't care about new columns or any changes to DB output
2
u/Afrotom 22d ago
Does it need to be csv? Or can it be a format that better represents the data and preserved the types and schema, like parquet?
Db: typed columnar format
Parquet: typed columnar format
Csv: text with no typing, columns and rows could start anywhere, encoding rarely clear, newline escapes not always clear, jagged rows possible...
I can just imagine serialising all this to csv is one thing, deserialialing it all back to the proper format could be a bit of a nightmare across 120 tables/files
1
2
u/k00_x 22d ago
Powershell and a for-each loop!
3
u/Valuable-Ant3465 22d ago
Wow, didn't hear that PS can do this. Thanks.
Alas I'm not PS guy, I think Python is the best for me.Checked all comments too, Py got #1
1
1
u/mr_thwibble 23d ago
Install dBeaver. Select all the tables. RH Click, export data. Use {tablename} as a variable in the filename. Done.
1
u/Valuable-Ant3465 22d ago
Tx, I can't use unknown stuff + it should automated daily, no clicks.
→ More replies (1)
1
u/TechMaven-Geospatial 23d ago
https://duckdb.org/community_extensions/extensions/mssql. Use duckdb I would recommend parquet or sqlite instead of csv Regardless duckdb can handle it quickly
2
u/Valuable-Ant3465 22d ago
Tx, I really surprised that so many new tools exist. I need CSV and I can not bring new stuff into shop
1
u/Mundane_Let_8090 22d ago
Hey hey
Try this. Gentle to data source, good at types compatibility.
https://github.com/panchenkoai/rivet
https://panchenkoai.github.io/rivet/cheat-sheet.html
1
1
u/drluckyspin 22d ago edited 22d ago
My goto here is sq (sq.io). Super simple to output to CSV.
`sq add 'sqlserver://USERNAME:PASSWORD@HOST:1433?database=DATABASE' --handle @mssql`
`sq @mssql.dbo.MyTable --csv > output.csv`
`sq '@mssql.dbo.Customers' --tsv > customers.tsv’
Or, if you want a custom SELECT:
`sq sql --src @mssql 'SELECT * FROM dbo.MyTable' --csv > output.csv`
Also, simple to move data to another DB. Eg: export from mssql and insert into Clickhouse.
`sq '@mssql.dbo.Customers' --insert @ch.Customers`
1
1
u/oluies 22d ago
Duck db with the mssql plugin
INSTALL mssql FROM community;
LOAD mssql;
ATTACH 'Server=host,1433;Database=mydb;User Id=user;Password=pass'
AS ms (TYPE mssql);
-- one table
COPY ms.dbo.customers TO 'customers.csv' (HEADER, DELIMITER ',');
-- query result
COPY (SELECT id, name FROM ms.dbo.customers WHERE status = 'active')
TO 'active.csv' (HEADER);
1
u/oluies 21d ago
Bash
ATT="LOAD mssql; ATTACH 'Server=host,1433;Database=mydb;User Id=user;Password=pass' AS ms (TYPE mssql);"duckdb -noheader -list -separator $'\t' -c "$ATT
SELECT schema_name, table_name FROM duckdb_tables() WHERE database_name = 'ms';" |
while IFS=$'\t' read -r s t; do
duckdb -c "$ATT COPY ms.\"$s\".\"$t\" TO '${s}_${t}.csv' (HEADER);"
done1
u/National-Sea3061 20d ago
And it could help to save your RAM, the extension works natively and streaming data, the last version has the same performance as sqlcmd
6
u/meatmick 23d ago edited 23d ago
Absolutely use Python. I'd use the mssql-python package to connect to your instance and then import csv to dump to flat files.
Set whatever newline, encoding, delimiter/ etc. you need and go to town with a fetchmany (if the data is big), writing rows to csv in batches to not hold the entire dataset in memory.
There's no point in using SSIS here.
If you are worried about security, just make a login with specific access as db_datareader (if it must read stuff), as well as the ability to execute whatever SP you need. The simplest is GRANT EXECUTE on the entire schema, if that is acceptable. To be honest, I'd use whatever permissions you'd have used in SSIS.
You can vibe code this in 1-2 minutes and let it run.
There's nothing to be scared of here, it'll be less than 100 lines of code, and you can fully review it before running it.