Skip to main content

SLAM_tables_to_csv

Overview

SLAM_tables_to_csv is a utility function that exports multiple SQL Server tables to CSV files by reading a list of table names from a database table. The procedure works entirely with SQL Server data and does not require any Salesforce connection.

The operation processes tables sequentially and continues processing even if individual tables fail, reporting any errors at the end.

Output Format:

  • Standard CSV format with comma delimiters, quoted fields, UTF-8 encoding
  • All columns from the table are exported
  • NULL values are written as empty strings
  • Files are named according to their table name with an optional suffix
  • By default, files are saved to the work directory.

Empty tables will generate CSV files with only column headers. The operation is logged in the SLAM_Activity_Log table (if configured).

Parameters

NameTypeDefaultDescription
@tablesVARCHAR(255)RequiredName of a table containing a single column with table names to export. Table names should be unqualified (e.g., Account not dbo.Account)
@suffixVARCHAR(128)''Suffix to append to each CSV filename (e.g., _export creates TableName_export.csv)
@output_dirVARCHAR(MAX)NULLOutput directory. If NULL, files are written to the work directory. Paths with spaces are fully supported (e.g., C:\data exports). Do not attempt to write files to folders the service account doesn't have permissions to (e.g., user folders in C:\Users\), as this will result in a permission error

Usage Examples

Basic Usage with Defaults

Export multiple tables with default settings:

-- Create list of tables to export
SELECT TABLE_NAME
INTO tables_to_export
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME LIKE 'Opportunity%'

-- Export all tables in the list
EXEC dbo.SLAM_tables_to_csv
@tables = 'tables_to_export'

Result: Creates CSV files (Opportunity.csv, OpportunityContactRole.csv, etc.) in the work directory

With Suffix

Add a suffix to distinguish this export batch:

EXEC dbo.SLAM_tables_to_csv
@tables = 'tables_to_export',
@suffix = '_backup'

Result: Creates Account_backup.csv, Contact_backup.csv, etc.

Custom Output Directory

Export to a specific directory:

EXEC dbo.SLAM_tables_to_csv
@tables = 'tables_to_export',
@output_dir = 'C:\data\exports'

Result: All CSV files are created in C:\data\exports\

All Parameters Specified (Complete Example)

-- Step 1: Drop existing table if it exists
IF OBJECT_ID('tables_to_export', 'U') IS NOT NULL
DROP TABLE tables_to_export

-- Step 2: Create list of tables to export
SELECT TABLE_NAME
INTO tables_to_export
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'dbo'
AND TABLE_NAME LIKE 'SF_%'

-- Step 3: Export with custom suffix and directory
EXEC dbo.SLAM_tables_to_csv
@tables = 'tables_to_export',
@suffix = '_export',
@output_dir = 'C:\custom folder'

Result: Creates SF_Account_export.csv, SF_Contact_export.csv, etc. in C:\custom folder\