Read attachments information
This example set demonstrates how to use the get_attachments endpoint.
This endpoint retrieves attachment metadata directly from the ServiceNow sys_attachment table, allowing fast server-side filtering on attachment-level attributes. Use it when you want to query attachments across a table without first filtering parent records. For parent-level filtering (slower per-parent processing), use the Get Attachments By Parent Row Search endpoint instead.
Use the Query parameter for server-side filters on the sys_attachment table (examples: file_nameENDSWITH.png, content_typeSTARTSWITHimage/, size_bytes>1048576, or date placeholders like <<today-2d,FUN_TO_DATETIME>>). You can also specify a ParentSysId to limit to a single parent record or use SysId for a specific attachment. Replace sample sys_id and table names with values from your instance.
Standard SQL query example
This is the base query accepted by the connector. To execute it in SQL Server, you have to pass it to the Data Gateway via a Linked Server. See how to accomplish this using the examples below.
-- Get all attachments for the parent table 'problem'
SELECT * FROM get_attachments WITH(TableName='problem')
-- Get attachments where file name ends with .png or .zip (attachment table filter)
SELECT * FROM get_attachments WITH(TableName='problem', Query='file_nameENDSWITH.png^ORfile_nameENDSWITH.zip')
-- Get all attachments for a specific parent record (by parent sys_id)
SELECT * FROM get_attachments WITH(TableName='problem', ParentSysId='62304320731823002728660c4cf6a7e8')
-- Get a single attachment metadata row by exact attachment sys_id
SELECT * FROM get_attachments WITH(SysId='4b2d41168396b21032ddb9f6feaad38f')
-- Get attachments uploaded today
SELECT * FROM get_attachments WITH(TableName='problem', Query='sys_created_on>=<<today,FUN_TO_DATETIME>>^sys_created_on<=<<today|~|yyyy-MM-dd 23:59:59,FUN_TO_DATETIME>>')
-- Get attachments uploaded in the last 2 days
SELECT * FROM get_attachments WITH(TableName='problem', Query='sys_created_on>=<<today-2d,FUN_TO_DATETIME>>')
-- Get attachments uploaded after a specific date
SELECT * FROM get_attachments WITH(TableName='problem', Query='sys_created_on>=2026-01-01')
-- Get attachments with exact file name match
SELECT * FROM get_attachments WITH(TableName='problem', Query='file_name=dump.png')
-- Get attachments where file name starts with 'dump'
SELECT * FROM get_attachments WITH(TableName='problem', Query='file_nameSTARTSWITHdump')
-- Get attachments where file name contains 'error'
SELECT * FROM get_attachments WITH(TableName='problem', Query='file_nameLIKEerror')
-- Get attachments where file name ends with .pdf
SELECT * FROM get_attachments WITH(TableName='problem', Query='file_nameENDSWITH.pdf')
-- Get only image attachments
SELECT * FROM get_attachments WITH(TableName='problem', Query='content_typeSTARTSWITHimage/')
-- Get attachments larger than 1 MB
SELECT * FROM get_attachments WITH(TableName='problem', Query='size_bytes>1048576')
-- Get attachments uploaded by a specific user
SELECT * FROM get_attachments WITH(TableName='problem', Query='sys_created_by=admin')
-- Order attachments by created date (ascending)
SELECT * FROM get_attachments WITH(TableName='problem', Query='ORDERBYsys_created_on')
-- Order attachments by size descending
SELECT * FROM get_attachments WITH(TableName='problem', Query='ORDERBYDESCsize_bytes')
Using OPENQUERY in SQL Server
SELECT * FROM OPENQUERY([LS_TO_SERVICENOW_IN_GATEWAY], '-- Get all attachments for the parent table ''problem''
SELECT * FROM get_attachments WITH(TableName=''problem'')
-- Get attachments where file name ends with .png or .zip (attachment table filter)
SELECT * FROM get_attachments WITH(TableName=''problem'', Query=''file_nameENDSWITH.png^ORfile_nameENDSWITH.zip'')
-- Get all attachments for a specific parent record (by parent sys_id)
SELECT * FROM get_attachments WITH(TableName=''problem'', ParentSysId=''62304320731823002728660c4cf6a7e8'')
-- Get a single attachment metadata row by exact attachment sys_id
SELECT * FROM get_attachments WITH(SysId=''4b2d41168396b21032ddb9f6feaad38f'')
-- Get attachments uploaded today
SELECT * FROM get_attachments WITH(TableName=''problem'', Query=''sys_created_on>=<<today,FUN_TO_DATETIME>>^sys_created_on<=<<today|~|yyyy-MM-dd 23:59:59,FUN_TO_DATETIME>>'')
-- Get attachments uploaded in the last 2 days
SELECT * FROM get_attachments WITH(TableName=''problem'', Query=''sys_created_on>=<<today-2d,FUN_TO_DATETIME>>'')
-- Get attachments uploaded after a specific date
SELECT * FROM get_attachments WITH(TableName=''problem'', Query=''sys_created_on>=2026-01-01'')
-- Get attachments with exact file name match
SELECT * FROM get_attachments WITH(TableName=''problem'', Query=''file_name=dump.png'')
-- Get attachments where file name starts with ''dump''
SELECT * FROM get_attachments WITH(TableName=''problem'', Query=''file_nameSTARTSWITHdump'')
-- Get attachments where file name contains ''error''
SELECT * FROM get_attachments WITH(TableName=''problem'', Query=''file_nameLIKEerror'')
-- Get attachments where file name ends with .pdf
SELECT * FROM get_attachments WITH(TableName=''problem'', Query=''file_nameENDSWITH.pdf'')
-- Get only image attachments
SELECT * FROM get_attachments WITH(TableName=''problem'', Query=''content_typeSTARTSWITHimage/'')
-- Get attachments larger than 1 MB
SELECT * FROM get_attachments WITH(TableName=''problem'', Query=''size_bytes>1048576'')
-- Get attachments uploaded by a specific user
SELECT * FROM get_attachments WITH(TableName=''problem'', Query=''sys_created_by=admin'')
-- Order attachments by created date (ascending)
SELECT * FROM get_attachments WITH(TableName=''problem'', Query=''ORDERBYsys_created_on'')
-- Order attachments by size descending
SELECT * FROM get_attachments WITH(TableName=''problem'', Query=''ORDERBYDESCsize_bytes'')')
Using EXEC in SQL Server (handling larger SQL text)
The major drawback of OPENQUERY is its inability to incorporate variables within SQL statements.
This often leads to the use of cumbersome dynamic SQL (with numerous ticks and escape characters).
Fortunately, starting with SQL 2005 and onwards, you can utilize the EXEC (your_sql) AT [LS_TO_SERVICENOW_IN_GATEWAY] syntax.
DECLARE @MyQuery NVARCHAR(MAX) = '-- Get all attachments for the parent table ''problem''
SELECT * FROM get_attachments WITH(TableName=''problem'')
-- Get attachments where file name ends with .png or .zip (attachment table filter)
SELECT * FROM get_attachments WITH(TableName=''problem'', Query=''file_nameENDSWITH.png^ORfile_nameENDSWITH.zip'')
-- Get all attachments for a specific parent record (by parent sys_id)
SELECT * FROM get_attachments WITH(TableName=''problem'', ParentSysId=''62304320731823002728660c4cf6a7e8'')
-- Get a single attachment metadata row by exact attachment sys_id
SELECT * FROM get_attachments WITH(SysId=''4b2d41168396b21032ddb9f6feaad38f'')
-- Get attachments uploaded today
SELECT * FROM get_attachments WITH(TableName=''problem'', Query=''sys_created_on>=<<today,FUN_TO_DATETIME>>^sys_created_on<=<<today|~|yyyy-MM-dd 23:59:59,FUN_TO_DATETIME>>'')
-- Get attachments uploaded in the last 2 days
SELECT * FROM get_attachments WITH(TableName=''problem'', Query=''sys_created_on>=<<today-2d,FUN_TO_DATETIME>>'')
-- Get attachments uploaded after a specific date
SELECT * FROM get_attachments WITH(TableName=''problem'', Query=''sys_created_on>=2026-01-01'')
-- Get attachments with exact file name match
SELECT * FROM get_attachments WITH(TableName=''problem'', Query=''file_name=dump.png'')
-- Get attachments where file name starts with ''dump''
SELECT * FROM get_attachments WITH(TableName=''problem'', Query=''file_nameSTARTSWITHdump'')
-- Get attachments where file name contains ''error''
SELECT * FROM get_attachments WITH(TableName=''problem'', Query=''file_nameLIKEerror'')
-- Get attachments where file name ends with .pdf
SELECT * FROM get_attachments WITH(TableName=''problem'', Query=''file_nameENDSWITH.pdf'')
-- Get only image attachments
SELECT * FROM get_attachments WITH(TableName=''problem'', Query=''content_typeSTARTSWITHimage/'')
-- Get attachments larger than 1 MB
SELECT * FROM get_attachments WITH(TableName=''problem'', Query=''size_bytes>1048576'')
-- Get attachments uploaded by a specific user
SELECT * FROM get_attachments WITH(TableName=''problem'', Query=''sys_created_by=admin'')
-- Order attachments by created date (ascending)
SELECT * FROM get_attachments WITH(TableName=''problem'', Query=''ORDERBYsys_created_on'')
-- Order attachments by size descending
SELECT * FROM get_attachments WITH(TableName=''problem'', Query=''ORDERBYDESCsize_bytes'')'
EXEC (@MyQuery) AT [LS_TO_SERVICENOW_IN_GATEWAY]