SQL Server guide

Upload an attachment


Upload a file and attach it to a specific record. ParentSysId refers to the parent record's sys_id (table_sys_id) where the attachment will be linked. Provide the target TableName, the ParentSysId, the desired FileName as it appears in ServiceNow, and the local SourceFilePath.

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.

-- Upload and attach local file to a 'incident' record 
SELECT * FROM upload_attachment 
WITH(
  TableName='incident',
  --below is sys_id from 'incident' table where you like to attach uploaded file
  ParentSysId='62304320731823002728660c4cf6a7e8',
  FileName='dump_saved.png',
  SourceFilePath='c:\temp\dump_saved.png'
)

Using OPENQUERY in SQL Server

SELECT * FROM OPENQUERY([LS_TO_SERVICENOW_IN_GATEWAY], '-- Upload and attach local file to a ''incident'' record 
SELECT * FROM upload_attachment 
WITH(
  TableName=''incident'',
  --below is sys_id from ''incident'' table where you like to attach uploaded file
  ParentSysId=''62304320731823002728660c4cf6a7e8'',
  FileName=''dump_saved.png'',
  SourceFilePath=''c:\temp\dump_saved.png''
)')

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) = '-- Upload and attach local file to a ''incident'' record 
SELECT * FROM upload_attachment 
WITH(
  TableName=''incident'',
  --below is sys_id from ''incident'' table where you like to attach uploaded file
  ParentSysId=''62304320731823002728660c4cf6a7e8'',
  FileName=''dump_saved.png'',
  SourceFilePath=''c:\temp\dump_saved.png''
)'
EXEC (@MyQuery) AT [LS_TO_SERVICENOW_IN_GATEWAY]