Skip to main content

SLAM_query

Overview​

SLAM_query reaches all three APIs and file downloading through one procedure. It calls the query procedure that matches @api, which is useful when the API is chosen at run time. @batch_size applies only to the SOAP API, and @pkchunksize only to the Bulk v1 API.

Parameters​

NameTypeDefaultDescription
@queryNVARCHARRequiredsObject name or SOQL query string. When just an sObject name is passed, queries all records & fields. The query can span several lines (see SOQL Language Support)
@tableVARCHARRequiredTarget SQL table name for results
@apiNVARCHARRequiredAPI to use ('bulk', 'bulkv2', or 'soap')
@pkchunksizeINTNULLEnable PK chunking (Bulk v1 API only). If NULL, PK chunking is disabled. Speediful warns when each chunk is large for the query and never changes the value. See Choosing a chunk size
@batch_sizeINT2000Number of records per batch (SOAP API only)
@queryallBIT0When 1, includes deleted/archived records
@exists_actionVARCHAR(20)NULLControls the behavior when the destination table name, specified by @table is already used. If NULL, uses the value from SLAM_Settings.default_exists_action (defaulted to 'drop' during initial setup).

Valid options:

  • 'drop' - Drop the existing object and create a new one (default)
  • 'rename' - Rename the existing object by appending a timestamp suffix (format: _YYYYMMDDTHHMMSS_MMM)
  • 'append' - Add the rows to the existing table and keep the rows already in it. See Appending to an existing table
  • 'fail' - Throws a SQL exception if the destination object already exists
  • 'drop-after' - Like 'drop', but keeps the existing object, renamed, until the run succeeds. See The drop-after option

Column types​

Speediful creates the destination table from the object's field definitions in Salesforce, not from the rows the query returns. Ids and lookups get nchar(18), text fields get nvarchar sized to the field's length, Date/Time fields get datetime2, and Checkbox fields get bit. Currency, Number and Percent fields get decimal columns sized by the numeric precision setting. A whole-number field that declares more than 9 digits gets bigint.

Appending to an existing table​

@exists_action = 'append' adds the rows to the existing table and keeps the rows already in it. If the table does not exist, Speediful creates it, so the first run with 'append' needs no setup.

If a query has more fields than fit in one SOQL query (see Wide Salesforce Tables), Speediful splits it and adds all its rows to the table in one step at the end. During that step:

  • In a database using the SIMPLE or BULK_LOGGED recovery model, Speediful holds an exclusive lock on the table, so other sessions that read or write the table wait until it finishes.
  • In a database using the FULL recovery model, the step is one transaction that needs free transaction log space for all the rows it adds.

When @exists_action = 'append' writes to a table that already exists, and incoming text for an nvarchar, varchar, nchar or char column is wider than the column, Speediful widens the column in place to the smallest width that fits (up to 4,000 characters, then MAX). Columns only grow, and Speediful never drops or changes your own indexes or constraints to widen one.

SQL Server refuses the widen in these cases:

  • The column is used in a computed column's expression, or by a schema-bound view or function.
  • The required width is MAX and the column is part of a primary key, a unique constraint, either side of a foreign key, or an index key.

Speediful then stops the run before it writes the batch that needed the wider column. The error names the table, the column, its current and required width, and the object that blocks the widen. Batches written before that point stay in the table.

The same applies to every query procedure, SLAM_replicate and SLAM_download_job.

Usage Examples​

Query using Bulk v1 API with PK Chunking:

EXEC dbo.SLAM_query
@query = 'Account',
@table = 'Account_query',
@api = 'bulk',
@pkchunksize = 100000

Query using SOAP API with custom batch size:

EXEC dbo.SLAM_query
@query = 'SELECT Id, Name, Email, Account.Name FROM Contact WHERE CreatedDate = TODAY',
@table = 'Contact_query',
@api = 'soap',
@batch_size = 1500

Query using Bulk v2 API for large datasets:

EXEC dbo.SLAM_query
@query = 'SELECT * FROM Opportunity WHERE CreatedDate > LAST_N_DAYS:365',
@table = 'Opportunity_query',
@api = 'bulkv2'

Query using a field function (see Field Functions):

EXEC dbo.SLAM_query
@query = 'SELECT Id, toLabel(StageName), FORMAT(Amount), Account.Name FROM Opportunity',
@table = 'Opportunity_query',
@api = 'bulk'