Greg Low from SQL Down Under released a new version of his tool called “SDU Tools”.
The tool contains many interesting and useful functions and stored procedures you can use in many projects.
For instance:
|
|

The code is fully documented itself and if you need more support or usage examples - you can find here.
CSV reader
What I was interested the most - it’s a new feature in the latest version 5.0 (August 2017).
The new function is ReadCSVFile.
I’ve decided to give it a closer look and test as it would be very useful for at least two reasons:
- Entire tool doesn’t need CLR enabled
- All you need to have is a SQL engine, without SSIS
Let’s get the party started
At the beginning, let’s do some simple test and prepare the file for that.
I’ve downloaded a CSV file that contains list of presidents of The USA.
[USPresident-Wikipedia-URLs-Thmbs-HS]
|
|

That is cool! Look’s almost perfect.
I would like to change the column names from ColumnXX to appropriate name from the file. Can I? There is a parameter called @RowsToSkip - let’s check it.
|
|
Closer. It skipped my header row, but column names remain as previously. Hence, there is no that option with this Stored Procedure to achieve that, but we apply one trick.
Rename columns
You can create (temporary) table with all 50 columns, exec procedure loading the outcome over there. But this doesn’t look funny:
|
|
Performance
From the beginning when Greg mentioned about this function (before its official release), I was very curious whether I’d be able to load bigger files, let’s say 500MB?
To be honest, I don’t believe in that as SQL engine would have to load entire file into memory first.
How long it takes now for those 44 rows?
|
|
Result:
|
|
I gonna multiplicate rows in the file and make 4400 of them.
|
|
We can already see that this solution won’t replace our good old SSIS.
Anyway, let’s do the last test with even more amount of rows to have a comparison.
|
|
On my laptop that tests took almost 3 minutes (2:52). Bad result.
I guessing that it wasn’t an intention of the author to provide smart replacement for SSIS for reading CSV files. However, you can use this method for dev purposes and to get a quick preview of CSV file.
Additional quotes and NULL
CSV is a Comma-Separated Values, right?
Hence you need to have a separator for rows and columns. Both different.
What if your data contains char using as a separator? In that scenarios the value should be encapsulated by additional characters:

Otherwise, the reader will interpret that value incorrectly (row id 5).
I only wanted to check if the tool which I’m testing today is resistant against that.
|
|

This is a very common scenario when people trying to understand what has happened and why the ETL process doesn’t load data correctly and/or why failing when tries to convert string-value to date or numeric value.
As you can see above - the procedure can’t cope such cases.
But, to be fair - I’ve never expected such complex processing in the function who do not use CLR/.NET libraries but purely T-SQL.
Summary
This function has certain limitation. But hey. It’s only T-SQL, there is no .NET code in there.
It can help you review or/and load some CSV files, but not very big and non-complex one.
Hopefully, the next release of this method will fix “quote-issue” mentioned above.
Resources
USPresident-Wikipedia - ZIP file (contains CSV files)
SDU Tools.