Wednesday, July 24, 2013

Bulk update of image data in MSSQL

Someone asked me how to update a column in MSSQL table with bulk insertion of compressed images data from a folder. How to achieve it and here is detailed code and steps to achieve the same

 /* create a folder C:\Images\ with all the compressed images */
USE GO

CREATE TABLE dbo.tblImages (
 id INT identity(1, 1) PRIMARY KEY
 ,FlowerName VARCHAR(100) NULL
 ,FlowerImage IMAGE
 ) GO

INSERT INTO dbo.tblImages (
 FlowerName
 ,FlowerImage
 )
SELECT 'Flower1'
 ,BulkColumn
FROM Openrowset(BULK 'C:\Images\1.jpg', Single_Blob) AS img GO

INSERT INTO dbo.tblImages (
 FlowerName
 ,FlowerImage
 )
SELECT 'Flower2'
 ,BulkColumn
FROM Openrowset(BULK 'C:\Images\2.jpg', Single_Blob) AS img GO

INSERT INTO dbo.tblImages (
 FlowerName
 ,FlowerImage
 )
SELECT 'Flower3'
 ,BulkColumn
FROM Openrowset(BULK 'C:\Images\2.jpg', Single_Blob) AS img GO

You can use a loop to do this automatically in SSIS or other ETL tool or Stored Proc. 

Also you can use some free SQL Server addins like SSMSBoost add-in - productivity tools for SSMS 2008 / 2012 (Sql Server Management Studio)and that will help you to see the image stored as binary in SSMS itself just to test 

http://www.ssmsboost.com/Content/images/Feature/ssms-add-in-results-grid-visualizers.png 

Any question Let me know

No comments: