I am trying to insert multiple images into a Image datatype column in sql server 2012.
The folder has multiple .jpg and .png files.
I am currently using this sql to insert one by one
How can I insert multiple images or the whole folder thanks.
INSERT INTO Images(ImageId, ItemId, Caption, Image)
Values(NEWID(), null, null,(
SELECT * FROM OPENROWSET(
BULK 'C:\Images\image004.jpg',
SINGLE_BLOB) AS x))
Here is the table with clustered index on the ClusterKey column
CREATE TABLE [dbo].[Images](
[ImageId] [uniqueidentifier] NOT NULL,
[ItemId] [uniqueidentifier] NULL,
[Caption] [nvarchar](100) NULL,
[Image] [image] NOT NULL,
[ClusteredKey] [int] IDENTITY(1,1) NOT NULL,
CONSTRAINT [PK_Images] PRIMARY KEY NONCLUSTERED
(
[ImageId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
Currently Trying this approach
--Table to process the files
CREATE TABLE imagelist
(
imgfilename VARCHAR(200)
)
GO
--Put all file name in a table for easy processing
DECLARE @SQL AS NVARCHAR(2000)
SET @SQL =N'xp_cmdshell ''dir c:\Images /B'''
INSERT INTO imagelist (imgfilename) EXEC sp_executesql @SQL
GO
--Import data into target table
DECLARE @SQL AS NVARCHAR(2000)
DECLARE @ImgFilename AS VARCHAR(200)
DECLARE filelist CURSOR FOR
SELECT imgfilename
FROM imagelist
OPEN filelist
FETCH next FROM filelist INTO @ImgFilename
WHILE ( @@FETCH_STATUS = 0 )
BEGIN
SET @SQL = 'INSERT INTO Images(ImageId, Image) Values(NEWID(),( select reverse(substring(REVERSE('''
+ @ImgFilename
+ ''') ,charindex(''.'',REVERSE('''
+ @ImgFilename + ''' ),1)+1,LEN('''
+ @ImgFilename
+ '''))) , (SELECT img.bulkcolumn FROM OPENROWSET(BULK ''c:\Images\'
+ @ImgFilename + ''',SINGLE_BLOB) AS img))'
EXEC Sp_executesql
@SQL
FETCH next FROM filelist INTO @ImgFilename
END
CLOSE filelist
DEALLOCATE filelist
ImageIdandImage, then you insert a GUID, a filename (without type) and the contents of the file. Three columns of data into two table columns?