Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 insert and retrieve image in Stored Procedure

Author  Topic 

noblemfd
Starting Member

38 Posts

Posted - 2014-04-14 : 16:56:57
I have the table shown below as tblStudent. Please how do I create a stored procedure that will insert into, and update the table. Expecially because of the image datatype.

Thanks
create table tblStudent
(
sno int primary key,
sname varchar(50),
course varchar(50),
fee money,
photo image
)

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2014-04-14 : 17:07:41
I suspect the problem you will have is not how to create the procedure(s) to insert and update the table. The problem you will have is from where is the image source content coming, right? Assuming you have an image file (like .jpg) on a file system then you will need some application to stream the content into your table or into in parameter of a stored procedure. With straight t-sql you can't access the binary file content from an image file and insert it into an image column.

A typical and simple way to handle this to NOT store the image in the database but rather store a reference to the file in the database. So things like FilePath, fileName, and fileSize would be stored in your database while the actual image would be in a file system.

Be One with the Optimizer
TG
Go to Top of Page

noblemfd
Starting Member

38 Posts

Posted - 2014-04-14 : 17:16:02
quote:
Originally posted by TG

I suspect the problem you will have is not how to create the procedure(s) to insert and update the table. The problem you will have is from where is the image source content coming, right? Assuming you have an image file (like .jpg) on a file system then you will need some application to stream the content into your table or into in parameter of a stored procedure. With straight t-sql you can't access the binary file content from an image file and insert it into an image column.

A typical and simple way to handle this to NOT store the image in the database but rather store a reference to the file in the database. So things like FilePath, fileName, and fileSize would be stored in your database while the actual image would be in a file system.

Be One with the Optimizer
TG



Please can you give me a clue or sample code to this or a sample query, as you stated above. Note that it will work on a LAN
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2014-04-14 : 17:22:21
what specifically do you want the sample code to illustrate?

>>Note that it will work on a LAN
What will work on a LAN? Can you post the code that "works" on a LAN?

EDIT:
Here is sample code for creating an SP:

if object_id('dbo.insUpd_tblStudent_sp') is not null
drop proc dbo.insUpd_tblStudent_sp
go

create proc insUpd_tblStudent_sp
@sno int,
@sname varchar(50),
@course varchar(50),
@fee money,
@photo image
as

update s set
photo = @photo
from tblStudent s
where sno = @sno

if @@rowcount = 0
begin
insert tblStudent
(sno,
sname,
course,
fee,
photo)
values (@sno,
@sname,
@course,
@fee,
@photo)
end

go
grant exec on dbo.insUpd_tblStudent_sp to <SomeUserOrRole>
go


Be One with the Optimizer
TG
Go to Top of Page

noblemfd
Starting Member

38 Posts

Posted - 2014-04-14 : 17:34:15
quote:
Originally posted by TG

what specifically do you want the sample code to illustrate?

>>Note that it will work on a LAN
What will work on a LAN? Can you post the code that "works" on a LAN?

EDIT:
Here is sample code for creating an SP:

if object_id('dbo.insUpd_tblStudent_sp') is not null
drop proc dbo.insUpd_tblStudent_sp
go

create proc insUpd_tblStudent_sp
@sno int,
@sname varchar(50),
@course varchar(50),
@fee money,
@photo image
as

update s set
photo = @photo
from tblStudent s
where sno = @sno

if @@rowcount = 0
begin
insert tblStudent
(sno,
sname,
course,
fee,
photo)
values (@sno,
@sname,
@course,
@fee,
@photo)
end

go
grant exec on dbo.insUpd_tblStudent_sp to <SomeUserOrRole>
go


Be One with the Optimizer
TG



Thanks a lot
Go to Top of Page
   

- Advertisement -