In this tutorial we will help you complete the task of importing or processing data from a Comma Separated Value (CSV) file.
We will demonstrate how to configure a task to complete this goal, and be able to quickly repeat the process time and time again.
In this first example we show you how to import to a table, and how to update a table from a CSV file, in a later example we explain how to send CSV data to a stored procedure.
Setting up the task is a do it once process - EDT saves all your task settings into a task XML file. The next time you want to repeat the process you simply select the Task from the main EDT interface.
Be aware before we start - What are the pitfalls of importing CSV data into SQL Server?
Often companies experience intermittent data import failures when trying to import CSV data.
So how can we make this a secure and reliable process? How can the EDT software help you?
Validation, validation, validation..
- With a visual interface that allows you to correct the data
- Automatic validation highlighting any problems on a per-row basis.
- Validation is not just data type validation - but validation with the destination database itself - this being wrapped in a transaction that is always undone on any validation problem.
- Any number of validation rules can be added.
- Data can be sent to a stored procedure for more advanced validation, the procedure can return validation error messages.
How does EDT compare to other import techniques?
Compared to any other import method EDT gives a huge advantage, and provides a highly reliable import method.
The reason companies have problems importing CSV data is the fact that fields may missing or contain invalid data that renders the whole file invalid. EDT efficiently overcomes all these import problems improving business efficiency and data accuracy.
Worked example - Transfer CSV data to SQL Server Table
Here we assume you have already downloaded the EDT software (you can run this example even with the trial version, please see download page).
In this example we will load, modify, validate and upload this sample worksheet:
We will see how EDT can help you to easily :
- highlight and reject rows with missing essential data
- validate values against existing list or sql data
Here is the example source data we will use for tutorial:
STEP 1 - Create New Task
Before creating a task we must first sign in to EDT to unlock its features.
The default Password to log in is ''lean''.
Once logged in select the ''New task'' option which has been made available.
Select ''Insert or update table from any source''
Now give the task a name and then click on new to connect to your destination SQL server database:
STEP 2 - Connecting to SQL Server
In this example we will use one of the Native SQL Server drivers:
Enter the essential connection details only :
- Name of your SQL Server
- Connection method
- Select Database name
- Test the connection
STEP 3 - Select destination table
- NOTE : The chosen destination partly determines the layout of columns in the task
- EDT can thought of as 'using Excel as a Workspace', the layout of the workspace is determined by the data Destination,
- the data displayed (from the data source) is overlayed on top of the destination layout and is determined on the 'Data source / SQL' configuration tab.
- A task does not have to have data source, data can be pasted into a task from the clipboard if you wish, but generally it is much better to use the data source tab as data can be imported from any source.
- Using the 'Destination columns / SQL tab screen' as shown above you can:
- Define task column layout using drag and drop of the column name
- Hide unwanted columns
- Exclude unwanted columns from the SQL
- Insert what we call 'Work columns' - a user defined column that can be used for several purposes - a calculated value for example.
- Apply additional validation rules (data type validation is automatic)
- Define display formatting, for example date or currency values
- Create 'SQL lookup' columns that can retrieve existing values from the database that match the imported data
- And more!
We will stick to the basics for the purpose of this tutorial.
STEP 5 Creating and using the ODBC source
||Using ODBC is just one way of connecting to a CSV / text file. You can use alternative connection methods, and you can paste a connection string into the connection string text box. Please see the EDT reference section CSV / Text File Connection Strings
To connect using ODBC, click the 'Data source / SQL' tab:
Click ODBC 32 to create a new ODBC connection to launch the windows ODBC administrator.
Click Add in the ODBC administrator:
- Provide a Data Source Name.
- Un-check the 'Use current directory' option and select the directory where your CSV file resides
- Select OK to close the ODBC administrator and return to EDT.
Now select provider Microsoft OLE DB Provider for ODBC Drivers, click Next..
Select the ODBC CSV data source that you created, click Test
Click OK to return to EDT:
As shown in the above screenshot you will now see now how easy and brilliantly flexible it is to select data from CSV file using EDT !
- You can select a specific CSV file using the Object Source dropdown
- SQL is generated for you and by default all columns are included
- If you click the 'Custom' SQL radial option - you can manually edit the SQL as you require
- You could modify the SQL to only select a sub set of the data for example by adding a WHERE clause
- The SQL can be modified to only select certain columns form the CSV file
- Add blank columns if you wish by using the NULL keyword eg SELECT [WebCustomerID], NULL, [Email]..
- Click the 'Test' button to check the SQL syntax and view the first 100 records
- You can click the Map Columns button to reorder the select list using drag and drop - showing you a mapping between the destination and source columns
- You can insert Task Parameters using the parameter insert button ( ) this allows you to load data matching user supplied parameters - such as a category or date range (User parameters are created using the task configuration Parameters tab)
STEP 7 - Column Mapping
Click the Map Columns button
In the example above we have initially aligned the columns that match exactly.
You can drag and drop to alter the position/order of both the source and destination columns.
We now need to exclude the unwanted columns, this will complete in step 8 and 9.
Notice the source has a First name and Last name, but the destination only has a Name field, this we will deal with in Step 10.
STEP 8 - Excluding unwanted Source columns
Click the custom SQL option and remove the unwanted source columns from the SQL:
Click Test SQL to verify the syntax and view the data that will be returned by the query:
STEP 9 - Excluding unwanted Destination columns
On the Destination tab for unwanted columns,
- uncheck the Mandatory option,
- uncheck the Include in SQL option
- uncheck the Visible option
Notice that the SQL on the right hand panel is automatically updated to include only the columns required.
STEP 10 - Custom SQL to concatenate First and Last name fields
To concatenate the first and last name into one column we simply modify the Source SQL to append two fields using the ampersand operator , separating with a space:
STEP 11 - Test the Task
Save the task and return to the main EDT interface, and we will see the data loaded and ready to be imported.
STEP 12 - Remove unwanted rows
Often a CSV file will contain unwanted rows, these might be blank rows or rows that contain subtotals for example.
As you can see in the above screenshot our data contains some blank rows at the end, these we will remove using the very useful SQL function ISNUMERIC() field to check for a value in column in the CSV filet.
STEP 13 - Add data validation rules
There are several ways that EDT handles data validation within your task - ensuring the data is of good quality and good news for your business!
- Data type validation (automatic)
- Mandatory / must have values
- One or more specific validation rules by column using the power of Excel Data validation
- Validation by batch - specifying that all rows rows must be valid before the Send button operation is used
- Validation using fixed lists or SQL lookup Lists
- Server validation - automatic by use of the Validation button
- Additional server validation - applicable when sending data to a stored procedure
In this example we will add three validation rules:
- Email address must be supplied
- Name bust be more than two characters long
- Country must match one of our existing entries the County table, and the allow the user to correct the data by selecting from a drop down list
Marking an item as Mandatory
This is very easy - just click the Mandatory checkbox next to the data item name:
When the data is loaded into the task, any missing mandatory data is highlighted.
Notice that the Active Legend Red status shows a count of two rows . You can use the legend up/down arrows to very quickly locate the rows in each status band.
The left hand column shows the user what is wrong, and the problem cell has a red border:-
Using the power of Excel Data Validation!
Here we will add a validation rule specifying the Name value must be more than three characters:
Additionally, on the Error Alert tab, enter an error description for user feedback:
Notice that multiple validation messages appear when on row 16 of this example, as two column values are invalid:
Validation with Lists
In our example we are importing a Country value, but it must match with a valid country name in an existing Country table on SQL server.
We can easily use EDT sql lookup column capability to validate these values.
Click the SQL list button on the Country row item :
Choose options as shown below to create a Lookup column:
The option shown at the bottom of the dialogue Show validation error of column value not in list must be ticked for lookup validation to take place.
Returning to EDT as you can see below, it's now obvious there are some spelling errors in the country name data:
The user can simply click on the country cell and select the correct country name from the drop down list!
STEP 14 - Using the Validation button
In order to validate the task data with the destination SQL server database, click the EDT Validate button:
Rows that are ready to be validated are automatically selected, rows with validation errors are not selected:
(note that the Batch tab allows you to configure this behaviour, you can also remove the user prompt on the More tab)
STEP 15 - Using the Send button
Click the send button commits the operation to the database actually inserts the new data, rows that have been sent change to status green:
Thats it! you have added the data to the database.
STEP 16 - Updating Existing data
It is quite possible you may wish to UPDATE data if a matching row already exists.
To do this we simply modify the SQL on the Destination tab:
In our example, if a company with a matching name exists, we will update the Email address only.
(In reality you will more likely be able to use a more reliable field such as company ID)
We hope we have demonstrated that the EDT software provides a powerful and yet easy to use solution to importing Comma Separated value (CSV) data - and we believe the most reliable import method.
Questions? - please comment or feedback below or contact us directly for more information.
Lean Software Team