Xbasic

a5_createSQLTablesFromExcelFile Function

Syntax

dim result as c = A5_createSQLTablesFromExcelFile(filename as c, connectionString as c [, mode as c [, tableNamePrefix as c]])

Arguments

filenameCharacter

The filename of the Excel file.

connectionStringCharacter

The connection string for the target Database.

modeCharacter

The action to take if an existing table in the target database is found. Can be 'skip' or 'overwrite'.

tableNamePrefixCharacter

A prefix to use for each table created in the target database.

Returns

resultCharacter

Returns a JSON string with information for each sheet in the Excel file (see example below)

Description

Creates tables in a database from an Excel file.

Discussion

The a5_createSQLTablesFromExcelFile() function allows you to export data from an Excel file to a SQL database.

  • The Excel file can have one or more sheets.
  • Each sheet in the Excel file will become a table in the SQL database.
  • Alpha Anywhere will automatically compute the schema for each table in the SQL database. Alpha Anywhere can determine if the data in the Excel file is character, numeric, dateTime or logical.
  • For character fields, Alpha Anywhere will automatically compute the appropriate size (by taking the length of the largest value in column and adding an additional 3 spaces). If the computed size is greater than 255, the field is designated as a longtext (i.e. memo field)
  • For numeric fields, Alpha Anywhere will automatically compute the field size and number of decimal places.
  • A column in the Excel file is considered to be a logical value if the column has only 1 or 2 unique values, and the unique values match one of the following values: 0, 1, yes, no, true, false, on, off
  • On each sheet, if the first column contains unique values, that column will be the table's primary key, otherwise an additional numeric, auto-increment field is added to the table and that field is designated as the table's primary key
  • If the first column in each sheet contains unique, monotonic, numeric values the primary key is designated as an auto-increment field.
  • When the tables are created in the target database, if an existing table is found, you can either overwrite the existing table or skip it.
  • You can specify an optional prefix for each table name in the target database. For example, if the sheet name in the Excel file is customers and the prefix is imported_, the table name in the database will be imported_customers.
  • The function will return a JSON string that indicates whether a table was successfully created in the target database for each sheet in the Excel file, and if a table was successfully created, how many records were imported into the table.

For example:

dim fn as c
fn= "C:\data\sampledata.xlsx"
cs = "ImportedTables"
mode = "overwrite"
tablenameprefix = ""
json = A5_createSQLTablesFromExcelFile(fn,cs,mode,tablenameprefix)

?json
=[
    { "tableName": "Sheet1", "tableCreated": true, "recordsImported": 2155, "importError": false
        }
]

If a table called 'Sheet1' already existed in the target database, and if the mode was set to "skip", the JSON returned by the function would be:

[
    {
        "tableName": "Sheet1",
        "tableCreated": false,
        "errortext": "Table already exists"
    }
]