I am trying to extract embedded attachment data from a SQL Server 2005 image data field. I have found one method that nearly works, which uses the BCP utility.
SET @filepath = 'N:\Attachments\document.pdf'
SET @cmd = 'bcp "select binaryImageField from server.database.schema.attachmentTable where id=100" queryout "'+@filepath+'" -T -n';
EXEC master..xp_cmdshell @cmd;
Putting this sql code inside a while loop enables me to extract all attachments into a file server directory. The only problem I am facing is that PDFs are the only attachments that actually open. JPG, DOC, XLS etc do not - they appear corrupted.
Can anybody shed some light on why one file format works here? Or is there a better utility to use to accomplish the same task?