I have some columns with image
data type and I want to preview (or browse) the data in those tables. When I use Select top 1000 rows
in SQL Server
I would write a proc (or query; see below) to export the binary out to the file system and then use any old off the shelf photo management utility (i.e. Windows Photo Viewer) to take a look at what's inside.
If your clever in your file naming you could give yourself enough information about each image in the name to quickly find it in the database again once you've visually located what your looking for.
Here is a proc that will export binary to the file system. I modified from this sample code. It's untested but should be extremely close for concept. It's using BCP to export your binary. Check here for the full docs on the BCP utility.
The proc also gives you the ability to export everything in the table, or only a single row based on a the passed primarykey. It uses a cursor (yuck), as well as some dynamic sql (yuck, yuck) but sometimes you gotta do what you gotta do.
CREATE PROCEDURE ExportMyImageFiles
(
@PriKey INT,
@OutputFilePath VARCHAR(500)
)
AS
BEGIN
DECLARE @sql VARCHAR(8000)
IF @PriKey IS NULL /* export all images */
BEGIN
DECLARE curExportBinaryImgs CURSOR FAST_FORWARD FOR
SELECT 'BCP "SELECT MyImage FROM [dbo].[MyTable]
WHERE PrimaryKey =' + CAST(PrimaryKey AS VARCHAR(25)) +
'" queryout ' + @OutputFilePath + MyImageName + '.' +
MyImageType + ' -S MyServer\MyInstance -T -fC:\Documents.fmt'
FROM [dbo].[MyTable]
OPEN curExportBinaryImgs
FETCH NEXT FROM curExportBinaryImgs INTO @sql
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC xp_cmdshell @sql, NO_OUTPUT
FETCH NEXT FROM curExportBinaryImgs INTO @sql
END
CLOSE curExportBinaryImgs
DEALLOCATE curExportBinaryImgs
END
ELSE /* Export only the primary key provided */
BEGIN
SELECT @sql = 'BCP "SELECT MyImage FROM [dbo].[MyTable]
WHERE PrimaryKey =' + CAST(PrimaryKey AS VARCHAR(25)) +
'" queryout ' + @OutputFilePath
+ MyImageName + '.' + MyImageType +
' -S MyServer\MyInstance -T -fC:\Documents.fmt'
FROM [dbo].[MyTable]
WHERE PrimaryKey = @PriKey
EXEC xp_cmdshell @sql,NO_OUTPUT
END
END
This is all assuming of course that what is stored in your Image column is actually an image and not some other file type. Hopefully if it is an image you also know the type, bmp, jpg, png, gif, etc.
If you don't want the hassle or reusability of a full blown proc try single query like this:
DECLARE @OutputFilePath VarChar(500) = /* put output dir here */
DECLARE @sql VARCHAR(8000)
DECLARE curExportBinaryImgs CURSOR FAST_FORWARD FOR
SELECT 'BCP "SELECT MyImage FROM [dbo].[MyTable]
WHERE PrimaryKey =' + CAST(PrimaryKey AS VARCHAR(25)) +
'" queryout ' + @OutputFilePath + MyImageName + '.' +
MyImageType + ' -S MyServer\MyInstance -T -fC:\Documents.fmt'
FROM [dbo].[MyTable]
OPEN curExportBinaryImgs
FETCH NEXT FROM curExportBinaryImgs INTO @sql
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC xp_cmdshell @sql, NO_OUTPUT
FETCH NEXT FROM curExportBinaryImgs INTO @sql
END
CLOSE curExportBinaryImgs
DEALLOCATE curExportBinaryImgs