Skip to main content

SLAM_Alpha_import_csvs

Overview​

Imports all CSV files from a specified folder into SQL Server tables. Each CSV file becomes a separate table, with automatic schema inference and optimized bulk loading. This is useful for batch importing data exports, migrating data from other systems, or loading reference data.

Only files with a .csv extension are imported. The file name determines the table name, and the first row is assumed to be the header for the column names.

Note: This operation requires valid Salesforce credentials for license validation. However, the profiling itself only accesses SQL Server data.

important

Requires SQL Server 2017 or later.

warning

The SQL Server service account must have read access to the specified folder. Do not use folders under user profiles (e.g., C:\Users\...) as the service account typically cannot access them. Use a shared location such as C:\data\csv_files.

Key Features:

  • Automatic Schema Inference: Detects column types (INT, FLOAT, DATETIME, BIT, NVARCHAR) from CSV data
  • Batch Processing: Imports all CSV files in a folder in one operation
  • Flexible Table Naming: Derive table names from filenames with optional suffix
  • Error Recovery: Continues processing all files even if some fail
  • Duplicate Column Handling: Automatically deduplicates column names with a suffix on import (e.g _1, _2 etc.)

Parameters​

ParameterTypeRequiredDefaultDescription
@folderVARCHAR(MAX)YesPath to folder containing CSV files (e.g., 'C:\data\csv_files')
@suffixVARCHAR(128)No''Suffix to append to table names (e.g., '_20260114')
@separatorVARCHAR(10)No','CSV field separator/delimiter
@quotecharVARCHAR(10)No'"'CSV quote character for text fields
@lineterminatorVARCHAR(10)No'\r\n'CSV line terminator (CRLF for Windows)
@encodingVARCHAR(50)No'utf-8'CSV file encoding (supports utf-8, latin1, etc.)
@exists_actionVARCHAR(20)NoSLAM_SettingsAction when destination table exists. See Exists Actions

Table Naming​

Tables are automatically named based on the CSV filename (without .csv extension) plus an optional suffix:

  • Account.csv → Account
  • Account.csv + @suffix='_20260114' → Account_20260114
  • Contact Data.csv + @suffix='_load' → Contact Data_load

Exists Actions​

The @exists_action parameter controls behavior when a destination table already exists. If not specified, uses the value from SLAM_Settings.default_exists_action (defaulted to 'drop' during initial setup).

ActionBehavior
dropDrop existing table and create new one (data loss)
renameRename existing table with timestamp suffix (e.g., _20260114_153045)
appendAppend CSV rows to existing table (schema must match exactly) or creates it if it doesn't exist
failCheck ALL destination tables before importing. If any table already exists, the entire operation fails and no CSVs are imported

Usage Examples​

Basic Import​

Import all CSV files from a folder:

EXEC dbo.SLAM_Alpha_import_csvs @folder = 'C:\data\csv_files';

With Table Suffix​

Add a date suffix to all imported tables:

EXEC dbo.SLAM_Alpha_import_csvs
@folder = 'C:\data\csv_files',
@suffix = '_20260114';

Alternate Encoding​

Import CSV files with Latin-1 encoding:

EXEC dbo.SLAM_Alpha_import_csvs
@folder = 'C:\data\csv_files',
@encoding = 'latin1';

Fail If Tables Exist​

Prevent accidental overwrites by failing if any destination table already exists. All destination tables are checked before any CSVs are imported. If any conflict is found, the entire operation fails immediately:

EXEC dbo.SLAM_Alpha_import_csvs
@folder = 'C:\data\csv_files',
@exists_action = 'fail';

Append to Existing Tables​

Append CSV data to existing tables. The CSV schema must exactly match the existing table. If a schema mismatch is detected for a file, that file is skipped and the remaining CSV files continue to be processed:

EXEC dbo.SLAM_Alpha_import_csvs
@folder = 'C:\data\csv_files',
@suffix = '_staging',
@exists_action = 'append';

Schema Inference​

Column types are automatically detected by scanning all CSV data:

CSV DataSQL Server TypeExample
Integer valuesINT42, -100, 0
Decimal valuesFLOAT3.14, -0.5, 1.23e10
Binary integersBITColumns containing only 0 and/or 1
Date/time stringsDATETIME2024-01-15, 2024-01-15 14:30:00
TextNVARCHAR(n)Account Name, 555-1234
Empty columnsNVARCHAR(1)(all NULL values)

Mixed Types: If a column has mixed data types (e.g., some numbers, some text), the column is promoted to the most general type (usually NVARCHAR).

String Lengths: NVARCHAR columns are sized based on the maximum observed string length in the CSV file.

Error Handling​

Processing continues for all CSV files even if individual files fail. After completion, the output reports:

  • Success count: Number of files successfully imported
  • Failed files: List of files that failed to import
  • Total records: Total rows imported across all files

Error Scenarios​

ScenarioCause
Partial failureSome files failed to import (see Messages pane for details)
Table existsAny destination table already exists and @exists_action='fail' (checked eagerly before import)
Schema mismatchCSV schema doesn't match existing table (when appending)
Folder not foundThe specified folder path doesn't exist or is inaccessible

Schema Mismatch​

When @exists_action='append', the CSV schema must exactly match the existing table:

Causes:

  • CSV has different columns than the table
  • CSV columns are in a different order
  • CSV column types don't match table types

Solution:

  • Drop the table first: DROP TABLE dbo.Account_staging
  • Use @exists_action='drop' to automatically drop and recreate
  • Use @exists_action='rename' to preserve old data

Multiple Failures: If multiple CSV files have schema mismatches, all failures are reported after processing completes.

Requirements​

  • License: Requires a valid license and a configured Salesforce connection (for license validation only. No Salesforce data is accessed)
  • CSV Format: Files must have a header row in the first line
  • File Extension: Only *.csv files are processed (case-insensitive)

Example Output​

EXEC dbo.SLAM_Alpha_import_csvs @folder = 'C:\data\csv_files';

Successful import:

Found 3 CSV files to import
Successfully imported Account.csv to dbo.Account (1,245 records)
Successfully imported Contact.csv to dbo.Contact (3,891 records)
Successfully imported Opportunity.csv to dbo.Opportunity (567 records)

Partial failure:

Found 3 CSV files to import
WARNING: Schema mismatch for Account.csv: CSV schema does not match table dbo.Account
Successfully imported Contact.csv to dbo.Contact (3,891 records)
Successfully imported Opportunity.csv to dbo.Opportunity (567 records)

See Also​