SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


how to insert image in sql server 2005


how to insert image in sql server 2005

Author
Message
sanjay.dakolia
sanjay.dakolia
SSC Rookie
SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)

Group: General Forum Members
Points: 41 Visits: 17
hi,
i want to insert image into sql server using sql query. please help.
thanks in advance
sanjay
bleroy
bleroy
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1673 Visits: 825
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
deepkt
deepkt
Mr or Mrs. 500
Mr or Mrs. 500 (519 reputation)Mr or Mrs. 500 (519 reputation)Mr or Mrs. 500 (519 reputation)Mr or Mrs. 500 (519 reputation)Mr or Mrs. 500 (519 reputation)Mr or Mrs. 500 (519 reputation)Mr or Mrs. 500 (519 reputation)Mr or Mrs. 500 (519 reputation)

Group: General Forum Members
Points: 519 Visits: 362
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
Lowell
Lowell
SSC Guru
SSC Guru (181K reputation)SSC Guru (181K reputation)SSC Guru (181K reputation)SSC Guru (181K reputation)SSC Guru (181K reputation)SSC Guru (181K reputation)SSC Guru (181K reputation)SSC Guru (181K reputation)

Group: General Forum Members
Points: 181783 Visits: 41569
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
--help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
DiverKas
DiverKas
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1067 Visits: 460
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.
Sean Lange
Sean Lange
SSC Guru
SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)

Group: General Forum Members
Points: 147423 Visits: 18564
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.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
MMartin1
MMartin1
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: General Forum Members
Points: 15888 Visits: 2079
Lowell's solution is a good one. The Image datatype is deprecated thus the need for varbinary(max).

----------------------------------------------------
How to post forum questions to get the best help
sanjay.dakolia
sanjay.dakolia
SSC Rookie
SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)

Group: General Forum Members
Points: 41 Visits: 17
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'.
Lowell
Lowell
SSC Guru
SSC Guru (181K reputation)SSC Guru (181K reputation)SSC Guru (181K reputation)SSC Guru (181K reputation)SSC Guru (181K reputation)SSC Guru (181K reputation)SSC Guru (181K reputation)SSC Guru (181K reputation)

Group: General Forum Members
Points: 181783 Visits: 41569
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
--help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
Sean Lange
Sean Lange
SSC Guru
SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)

Group: General Forum Members
Points: 147423 Visits: 18564
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.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum







































































































































































SQLServerCentral


Search