ODBC guide

Download an attachment by ID


This example demonstrates how to use the download_attachment endpoint to download a single attachment file by its exact sys_id. Unlike bulk download endpoints, this allows you to specify a custom TargetFilePath for precise control over the saved file name and location. It does not support filters, as it targets one specific attachment.

Specify the attachment's SysId, the full TargetFilePath (e.g., c:\temp\myfile.png), and FileOverwriteMode to control behavior if the file exists (0 or 'AlwaysOverwrite' to overwrite, 1 or 'FailIfExists' to fail, 2 or 'SkipIfExists' to skip). Replace the sample sys_id with a valid attachment ID from your instance.

-- Download a single attachment by sys_id to a custom file path (always overwrite if exists)
SELECT * FROM download_attachment 
WITH(
    SysId='4b2d41168396b21032ddb9f6feaad38f', 
    TargetFilePath='c:\temp\dump_saved.png', 
	
	--Set overwrite mode : 0=AlwaysOverwrite, 1=FailIfExists, 2=SkipIfExists
    FileOverwriteMode=0 
)

-- Download another attachment with a different name (fail if file exists)
SELECT * FROM download_attachment 
WITH(
    SysId='another_attachment_sys_id_here', 
    TargetFilePath='c:\downloads\report.pdf', 
    FileOverwriteMode='FailIfExists'
)

-- Download using numeric value for skip if exists
SELECT * FROM download_attachment 
WITH(
    SysId='third_attachment_sys_id_here', 
    TargetFilePath='c:\files\data.xlsx', 
    FileOverwriteMode=2
)