I'm trying to export images from SQL server using bcr, I am also wondering how to do it so I decided to try it by myself. So my question is how can I export images from mssql succesfully, and what I did so far is described here:
On the command line I created file formatfile-n.fmt
and located it to E:
disk, because I searched the web a lot and I saw that its needed to edit that file to set it's SQLBINARY value from 8 to 0. Read that at this post (we are talking about images, that's reason why we need to edit SQLBINARY).
I executed command (below) and I exported images to location I wanted to, all images are there, but they have no size!
Here is my full code when images has no size:
EXEC sp_configure 'show advanced options', 1
GO RECONFIGURE GO EXEC sp_configure 'xp_cmdshell', 1 GO RECONFIGURE GO
EXEC master..xp_cmdshell 'mkdir C:\ImagesFromSql'
DECLARE @ID as int DECLARE @SQL as varchar(4000)
DECLARE csr CURSOR FOR SELECT BrojIskaznice FROM [Savez].[dbo].[Igraci]
OPEN csr
FETCH NEXT FROM csr INTO @ID
WHILE @@FETCH_STATUS = 0 BEGIN
SET @SQL = 'BCP "SELECT Fotografija FROM [Savez].[dbo].[Igraci] WHERE BrojIskaznice = "' + CAST(@ID as varchar(10)) + ' queryout "C:\ImagesFromSql\' + CAST(@ID as varchar(10)) + '.jpg" -T -fC "E:\formatfile-n.fmt'
EXEC master..xp_cmdshell @SQL
FETCH NEXT FROM csr INTO @ID
END
CLOSE csr DEALLOCATE csr
And here is my full code when Images has original size. But they can not be opened at all! like they are corrupted, that's the reason why I'm coming back to this post, but I did this already! changed value to zero!
EXEC sp_configure 'show advanced options', 1
GO RECONFIGURE GO EXEC sp_configure 'xp_cmdshell', 1 GO RECONFIGURE GO
EXEC master..xp_cmdshell 'mkdir C:\ImagesFromSql'
DECLARE @ID as int DECLARE @SQL as varchar(4000)
DECLARE csr CURSOR FOR SELECT BrojIskaznice FROM [Savez].[dbo].[Igraci]
OPEN csr
FETCH NEXT FROM csr INTO @ID
WHILE @@FETCH_STATUS = 0 BEGIN
SET @SQL = 'BCP "SELECT Fotografija FROM [Savez].[dbo].[Igraci] WHERE BrojIskaznice = "' + CAST(@ID as varchar(10)) + ' queryout "C:\ImagesFromSql\' + CAST(@ID as varchar(10)) + '.jpg" -T -N'
EXEC master..xp_cmdshell @SQL
FETCH NEXT FROM csr INTO @ID
END
CLOSE csr DEALLOCATE csr
I am really stuck here, this is so annoying to work with, does anyone have any advice?