0% found this document useful (0 votes)
101 views

SQL Server 2008 Varbinary (Max)

The document discusses storing and retrieving binary large objects (BLOBs) like images in SQL Server using the varbinary(max) data type. It provides steps to insert image files into a table with a varbinary(max) column, and then export the image back out of the database to the hard disk by creating a format file and using bcp and xp_cmdshell commands.

Uploaded by

Girish PS
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
101 views

SQL Server 2008 Varbinary (Max)

The document discusses storing and retrieving binary large objects (BLOBs) like images in SQL Server using the varbinary(max) data type. It provides steps to insert image files into a table with a varbinary(max) column, and then export the image back out of the database to the hard disk by creating a format file and using bcp and xp_cmdshell commands.

Uploaded by

Girish PS
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Varbinary(max) data type – SQL SERVER 2005 and later

It is used to store Binary large Objects (BLOB) like images and other data files.Data will be stored as bits
in the database. This is preferred for files having size less then 1 MB. For larger files use varbinary(max)
filestream.

Steps to use varbinary to store and retrieve BLOB – Images in SQL Server 2008.

I. Inserting data.

1. Create a table with varbinary column.

-- create a table and insert export image file into it.

 
CREATE TABLE dbo.ImageWarehouse
(
ImageWarehouseID INT IDENTITY (1,1) PRIMARY KEY,
ImageName VARCHAR(100),
  Photo VARBINARY(MAX)
)

2. Insert data in to the table


 

-- SQL Server import image - sql storing images database sql server

 
INSERT INTO dbo.ImageWarehouse (ImageName, Photo)  

SELECT
'IMAGE.JPG',
*
FROM   OPENROWSET
(
BULK ‘C:\file.jpg',SINGLE_BLOB
) AS x

 
II. Reading data.

1. Create a format file.


 
Go to command prompt and create a format file as given below

bcp "select top 1 ImgData from tblImages" queryout "c:\test.jpg" -T

Enter the file storage type of field img [nvarchar]:  {enter}

Enter prefix-length of field img [2]: 0

Enter length of field img [0]: {enter}

Enter field terminator [none]: {enter}

Do you want to save this format information in a file? [Y/n] y

Host filename [bcp.fmt]: C:\myFinal.fmt

2. Import binary data to HDD

Import the binary image to the HDD using the format file

 
-- SQL Server export image

DECLARE  @SQLcommand NVARCHAR(4000)

-- Keep the command on ONE LINE - SINGLE LINE!!! - broken here for
presentation

 
SET @SQLcommand = 'bcp "SELECT Photo FROM XLB4000_MASTER.dbo.ImageWarehouse" 
queryout "C:\myphoto.jpg" -T -f "C:\myFinal.fmt"'

PRINT @SQLcommand -- debugging

 
EXEC xp_cmdshell   @SQLcommand
NOTE:

To execute the xp_cmdshell command enable the advanced option as explained below.

EXEC master.dbo.sp_configure 'show advanced options', 1

RECONFIGURE

EXEC master.dbo.sp_configure 'xp_cmdshell', 1

RECONFIGURE

You might also like