Manually setup a mapping of Excel columns to fields in a SQL Server table

Create a new Database Mapping
Selecting the cells that should be exported
Mapping Excel Columns to Database Fields in SQL Server
Adding Drop Down Lists for columns
Use an Excel formula to update the value in a database column
Enabling Conflict Detection
Export Settings
Executing SQL code to manipulate the data that has been updated in SQL Server

Create a new Database Mapping


NOTE: You will have to also create a Database Import that will load the data from SQL Server into Excel, the Database Mapping only exports the data from Excel to SQL Server.

NOTE: For a quick start, use the SQL Spreads Designer to create a setup and then modify the Database Import and Database Mapping that was created by the Designer.
  1. Click the Database Mapping button in the SQL Spreads tab in Excel.


    Note: To enable the Advanced Setup menu, go to Document Settings > Other Settings tab and check the Show Advanced Setup menu checkbox.  

  2. This will bring up the Database Mappings dialog where you can see your existing Database Mappings.

  3. Click New.


  4. Select the cells that should be exported to SQL Server. If all rows should be exported, just select the header row.


  5. Then in the drop-down lists, select the Database and Table where the data should be updated.


  6. SQL Spreads will try to automatically map the spreadsheet columns to the database fields. Check that the mapping is fine and click OK.


  7. In the Database Mappings dialog, click OK again.

  8. Click Refresh.


  9. Do a few changes to the data in the exported cells and click the Save button to write your changes back to SQL Server.

  10. Go to SQL Server Management Studio to validate that the data was updated in SQL Server.
NOTE: If your columns in Excel has the same header name as the fields in the database, SQL Spreads will automatically map the spreadsheet columns to the database fields.

NOTE:
You can create several Database Mappings in the same Excel document.

Selecting which cells that should be exported


SQL Spreads can either export all rows below the header row, or a specified number of rows. 

To Export all rows
  1. Select the header row (or the first row of data if you do not have a header row. Then also uncheck the 'First row contains headers' checkbox)


  2. Check the Export all rows below selection that contains data checkbox at the bottom of the Database Mapping dialog:
To export a specific range of cells
  1. Select the range of cells in Excel to export. In this example, three columns in row 5 to 14 will be exported.


  2. Uncheck the Export all rows below selection that contains data check box

Mapping Excel Columns to Database Fields in SQL Server 


In SQL Spreads you can map exactly which columns in Excel that should be exported to specific fields in the database table. SQL Spreads also has a few built-in application columns that can insert application data into a field in the database table. 

To map a column in Excel to a field in the SQL Server table, select the Excel column in the drop-down for the database field.


NOTE: If you use a Header row in Excel with the same column names as the fields in the database, SQL Spreads automatically map the Excel columns to the database fields.

The  Application columns are: 

 Last Saved By 
Will insert the users Windows Login name.

Last Saved Time
Will insert the current date and time when the row was updated.

 Do Not Use
Will not insert any data into the database field

NOTE: If a database field is set to Do Not Use and you allow users to add new rows, you must set a default value for the field in the database. 

Unmapped Columns
If SQL Spreads cannot automatically map the column, it will be set the ***Unmapped Colum***

Insert Data From Filter
If you have created an Import Data Filter, you can let SQL Spreads insert the selected filter value into a column in the database table.

To map a Database Column to a selected filter value, select the filter item in the DropDown List under the section --- Insert Value From Filter ---. 

Adding Drop Down Lists for columns

SQL Spreads can create Drop Down Lists for columns that should contain a key value. The user will then select a Text value in a DropDown List and the corresponding key value will be inserted into the database table. 

To create a Drop Down List for a column:
  1. In the Database Mapping dialog, click the Add button on the right side for the column that should contain a DropDown List: 


  2. In the 'Edit Drop Down List' dialog, select the table that the Drop Down List values should be fetched from.


  3. Then select the column in the table that contains the key values.

  4. Now select which columns that should be shown to the user in the Drop Down list. You can select two columns and add a separator between them. In the example above the user will see the Account number followed by a hyphen and then the Account Description, e.g. '1110-Cash'.

  5. Click OK
Note: When importing data, you must import the data in the same format as you show the data in the Drop Down list. In the example above, you would need to create a JOIN to the DimAccount table and import the text  '1110-Cash' into the ParentAccountKey column. 

Use an Excel formula to update the value in a database column


There is a feature in SQL Spreads that allows you to use a formula in Excel to calculate the value that should be stored in a column in SQL Server. 


The feature is called Formula Columns and will give you a blank column where you can add a formula that calculates a value based on other columns values.

Follow these steps to set up a column in SQL Spreads as a Formula Column:
  1. Create a setup using the SQL Spreads designer or manually.

  2. Go to the Advanced Setup menu, select Database Import and edit the Database Import for your setup.
    For the column where you would like to add an Excel formula, set the column alias to FormulaColumn. Note that you must include the AS keyword when setting the alias:


  3. Go to the Database Mapping and check the “Always update all columns” checkbox at the bottom. This will force SQL Spreads to always update all columns in a row. (When saving to the database, SQL Spreads by default only updates the columns in the database for which the user has changed a value in Excel)


  4. Add a formula to the column(s) in Excel.

  5. Click the Refresh button in SQL Spreads and check that the formula is preserved in the column and not overwritten by the value from the database.

  6. Change a value that will affect the formula, then save to the database and check that the result from the formula is saved to the database.
Note:When setting a column alias to FormulaColumn, you must include the AS the keyword.
 
Formulas that depends on values outside of the database row
If you have formulas that depend on values in cells that are not in the same database row, you can force SQL Spreads to always update all rows by checking the "Update for all rows". In that way, you can have a formula with eg an index that should be applied to all rows.
 

Using several Formula Columns in a row
If you have several Formula Columns, set the alias to a FormulaColumn1, FormulaColumn2 etc.

Enabling Conflict Detection

SQL Spreads has a built-in Conflict Detection that will check if a row has been updated by another user since the last time the data was loaded into the spreadsheet. 

If a conflicting row is detected, SQL Spreads will show a dialog with the actual conflicting rows in the database and in the spreadsheet. The user will have the option to Overwrite or keep the conflicting changes. 

To enable conflict detection: 

  1. Check the Enable Conflict Detection checkbox


  2. In the drop-down list Spreadsheet Column containing Last Save Date Time, select the Excel Column that contains the date and time when the row was modified.

NOTE: To use conflict detection, you must have a column in your SQL Server table that stores the last time when the row was updated. You must include this row when imorting the data into the Excel spreadsheet.

NOTE: To automatically update the database table column with the time when a row was changed, map the column to the Application column Last Saved Time

Settings


First row contains headers
If checked, SQL Spreads will use the 
column names in the first row: 



If uncheck, SQL Spreads will use the Excel column names, like Column - A, Column - B etc.

Export all rows below selection that contains data
If selected, SQL Spreads will grab all the rows with data below the selection and export them to the SQL Server table. See the section Selecting which cells that should be exported above for more details.

NOTE: If you would like to export all rows with data in a sheet, just select the header row, and then check the Export all rows below selection that contains data checkbox.

Allow Insert New Rows
If checked, the user will be allowed to insert new rows in the spreadsheet, and the rows will be inserted in the database table.

Allow Delete Rows
If checked, the user will be allowed to delete rows in the spreadsheet, and the rows will also be deleted in the database table.

Executing SQL code to manipulate the data that has been updated in SQL Server

If you need to manipulate the data that has been updated in SQL Server, you can create a Post-Save SQL Query in SQL Spreads that will be executed after the Excel data has been updated in the SQL Server tables. 

To add a Post-Save SQL Query:

  1. In the SQL Spreads tab, click the Document Settings > Other Settings tab and click the Edit Post-Save SQL Query button.
  2. In the dialog, enter your query and click OK.

NOTE: The Post-Save SQL Query is executed in the same transaction as the update of the table data. This means that if your Post-Save SQL Query fails to execute, the update of the Excel data in your database tables will be rolled back and undone.


Feedback and Knowledge Base