Skip to main content

SLAM_query_by_id

Overview​

This query filters Salesforce data by the IDs in a filter table. It batches the IDs to stay within SOQL length limits, filters with an IN clause, and handles large sets of IDs.

The most common use-cases will apply to Salesforce objects that require filters when querying, e.g. ContentDocumentLink

You may also use this procedure to filter by other text fields, such as external ids. You must ensure any data is properly escaped as required by Salesforce

Performance tip: This procedure is designed for querying specific records when you have a list of IDs. It automatically batches IDs to stay within SOQL length limits and can handle large sets of IDs.

Parameters​

NameTypeDefaultDescription
@queryNVARCHARRequiredsObject name or SOQL query string. When just an sObject name is passed, queries all records & fields
@tableVARCHARRequiredTarget SQL table name for results
@filter_tableVARCHARRequiredFilter table containing IDs to filter by (must have an ID column)
@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

Usage Examples​

Query ContentDocumentLink records by ContentDocumentId using a filter table:

-- prepare filter table from ContentDocument records
EXEC dbo.SLAM_query_soap
@query = 'SELECT Id FROM ContentDocument LIMIT 600',
@table = 'ContentDocument_query'

CREATE TABLE ContentDocumentLink_filter(ContentDocumentId VARCHAR(18))
INSERT INTO ContentDocumentLink_filter(ContentDocumentId)
SELECT Id FROM ContentDocument_query

EXEC dbo.SLAM_query_by_id
@query = 'ContentDocumentLink',
@table = 'ContentDocumentLink_filtered',
@filter_table = 'ContentDocumentLink_filter'

Query with deleted records included:

-- prepare filter table
CREATE TABLE Lead_filter(Id VARCHAR(18))
INSERT INTO Lead_filter(Id)
<OUTPUT STATEMENT>

-- execute query
EXEC dbo.SLAM_query_by_id
@query = 'SELECT Id, Name, Email, Status, IsDeleted FROM Lead',
@table = 'Lead_filtered',
@filter_table = 'Lead_filter',
@queryall = 1

Query specific Contact fields using external ID filter:

-- prepare filter table
CREATE TABLE Contact_External_IDs(External_Id__c VARCHAR(255))
INSERT INTO Contact_External_IDs(External_Id__c)
VALUES ('ExtId123'), ('ExtId456')

-- execute query
EXEC dbo.SLAM_query_by_id
@query = 'SELECT Id, Name, Email, Account.Name FROM Contact',
@table = 'Contact_filtered',
@filter_table = 'Contact_External_IDs'