I'm trying to make sql script that will add to a table some rows with different data types in each field.
table has 4 columns - ID, first name, last name, image
I have a csv file with about 4000 rows with those columns (last one as a path to image file):
ex.
1,'John','Smith','d:\images\johnsmith.jpg'
I can add single row with this code:
INSERT INTO [Employee] ([ID],[First_Name],[Last_Name],[Image])
SELECT
1 AS [ID]
,'John' AS [First_Name]
,'Smith' AS [Last_Name]
, * FROM OPENROWSET(BULK 'd:\images\johnsmith.jpg', SINGLE_BLOB) AS [Image];
GO
But I would like to import this that like this
BULK INSERT [Employee]
FROM 'd:\tmp\data.csv'
WITH
(
FIRSTROW = 2,
FIELDTERMINATOR = ',', --CSV field delimiter
ROWTERMINATOR = '\n', --Use to shift the control to next row
TABLOCK
)
GO
however with this method I cannot not convert filepath to image data type. Maybe there is a way to run a command saved in csv fil, so I could make the file something like:
1,'John','Smith','* FROM OPENROWSET(BULK 'd:\images\johnsmith.jpg', SINGLE_BLOB)'
Is there any simple way to do this? I'm not very good with sql