Beyondrelational

Wednesday, June 9, 2010

Insert Picture into SQL Server 2005 Image Field using only SQL

USE AdventureWorks

-- export
DECLARE @SQLcommand nvarchar(4000)
SET @SQLcommand = 'bcp "select LargePhoto from AdventureWorks.Production.ProductPhoto where ProductPhotoID = 111" queryout "C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Blue hills.jpg" -T -n '

EXEC xp_cmdshell @SQLcommand

GO


-- import image
INSERT Production.ProductPhoto (
ThumbNailPhoto,
ThumbnailPhotoFileName,
LargePhoto,
LargePhotoFileName)
SELECT null, null, LargePhoto.*, N'Blue hills.jpg'
FROM OPENROWSET
(BULK 'C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Blue hills.jpg', SINGLE_BLOB) LargePhoto
GO

select * from Production.ProductPhoto order by ModifiedDate desc
GO

No comments:

Post a Comment