How to Import Image File from File System to SQL Server Table - TSQL Tutorial

To save Image file in a SQL Server Table first of all we need to have a column that can handle Image files. VARBINARY is data type that we can use to store Images or any other type of files. Let's create the Table with Varbinary Datatype and then import the image by using below script.

CREATE TABLE dbo.TblPicture_Src (
 ID INT Identity(1, 1)
 ,NAME VARCHAR(100)
 ,Picture VARBINARY(MAX)
 )
GO

--Import Image/File from File System To SQL Server Table
INSERT INTO dbo.TblPicture_Src (
 NAME
 ,Picture
 )
SELECT 'Capture1.PNG'
 ,BulkColumn
FROM Openrowset(BULK 'C:\4_Mypicture.PNG', Single_Blob) AS Picture

SELECT *
FROM dbo.TblPicture_Src


Fig 1: How to Import Files to SQL Server Table by using TSQL in SQL Server

As you can see that we used OpenRowset to import the file to SQL Server table. To make this script work, Change the file path according to your input file.


We can also use SQL Server Integration Services (SSIS) Package to load the image or any other files from file system to SQL Server Table by using Import Column Transformation.



How to Import Files to SQL Server Table by SSIS  Package

1 comment:

  1. You completely match our expectation and the variety of our information.

    notepad++ mac dmg

    ReplyDelete