November 5, 2012 at 5:08 am
hi,
i want to insert image into sql server using sql query. please help.
thanks in advance
sanjay
November 5, 2012 at 5:19 am
Hi,
you need to place your question in the SQL Server 2005 forum (http://www.sqlservercentral.com/Forums/Forum338-1.aspx instead of the 2008 forum.
B
November 5, 2012 at 6:46 am
Try this,
CREATE TABLE Img(Id INT, Obj VARBINARY(MAX))
INSERT INTO Img (Id ,Obj)
VALUES (1,(SELECT * FROM OPENROWSET(BULK 'c:\windows\Blue Lace 16.bmp', SINGLE_BLOB) Obj))
DROP TABLE Img
November 5, 2012 at 6:52 am
you would want to save the filename as well, don't forget....
deepkt (11/5/2012)
Try this,CREATE TABLE Img(Id INT, Filename varchar(255), Obj VARBINARY(MAX))
INSERT INTO Img (Id ,FileName,Obj)
VALUES (1,'Blue Lace 16.bmp',(SELECT * FROM OPENROWSET(BULK 'c:\windows\Blue Lace 16.bmp', SINGLE_BLOB) Obj))
DROP TABLE Img
Lowell
November 5, 2012 at 8:30 am
sanjay.dakolia (11/5/2012)
hi,i want to insert image into sql server using sql query. please help.
thanks in advance
sanjay
It is generally considered bad practice to store images in the SQL Server DB. Its pretty inefficient. You are better off storing the image in the file system and storing the path in the database.
November 5, 2012 at 8:54 am
DiverKas (11/5/2012)
sanjay.dakolia (11/5/2012)
hi,i want to insert image into sql server using sql query. please help.
thanks in advance
sanjay
It is generally considered bad practice to store images in the SQL Server DB. Its pretty inefficient. You are better off storing the image in the file system and storing the path in the database.
You might also look into FILESTREAM.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
November 6, 2012 at 12:02 pm
Lowell's solution is a good one. The Image datatype is deprecated thus the need for varbinary(max).
----------------------------------------------------
November 10, 2012 at 5:35 am
hi if i am using this query in a storeprocedure i am getting error. here is the sp and the error
USE [TEMP]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[psp_TEMP]
-- Add the parameters for the stored procedure here
@MimeType varchar(50),
@image image,
@Hid int,
@ImageName varchar(100),
@ImageDesc varchar(100)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
insert into image
(
MimeType,
image
)
select @MimeType,BulkColumn from Openrowset(Bulk @image , Single_Blob) as EmployeePicture
--values
--(
--@MimeType,
--@image
--)
declare @imageId int
set @imageId = @@identity
insert into tables
(
HistoryID,
ImageID,
ImageName,
ImageDescription
)
values
(
@Hid,
@imageId,
@ImageName,
@ImageDesc
)
END
i am getting the below given error :
Msg 102, Level 15, State 1, Procedure csp_Images_History_Insert, Line 20
Incorrect syntax near '@image'.
November 11, 2012 at 4:45 am
Sanjay openrowset does not allow variables...it must be a static string inside single quotes. Replace @image with 'c:/filename'
You could build an openrowset as a string and use dynamic sql instead
Lowell
November 12, 2012 at 7:01 am
And don't forget as previously mentioned that the image datatype is deprecated. Instead use varbinary(max). It will behave the same way but is the newer preferred datatype for this.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply