Click here to Skip to main content
15,917,731 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two table .. ImageTable and ImageDetails
ImageTable has two columns .. Sno which is identity and img which is varBinary. I am storing images in ImageTable in varBinary format.

I am inserting details in ImageDetails which also include a column called HoMakerId which is a foreign key to ImageTable's Sno. So everything i am inserting image into ImageTable i am also inserting the Sno of last inserted image in ImageDetails ... something like this
CLICK HERE[^]


SQL
GO
ALTER procedure [dbo].[sp_InsertImage]
@Image varbinary(max),
@Date datetime,
@Uploader int
as
Insert into ImageTable(img) values(@Image);


Insert into ImageDetails(makerid,createdon,homakerid) values(@Uploader,@Date,(select top 1 sno from imagetable order by sno desc))


Do you think i need another approach or this one is fine? Should i be using transactions?
Posted
Comments
ali_heidari_ 19-Aug-13 13:25pm    
do you have problem with your store procedure?
arbaaz jalil 19-Aug-13 13:26pm    
No its working fine. I just want to know if i should use another approach to store sno of first table in second table's HoMakerId.

1 solution

Use @ScopeIdentity. Change sp like this
SQL
Insert into ImageTable(img) values(@Image);
DECLARE @Id INTEGER
SET @Id = SCOPE_IDENTITY()
Insert into ImageDetails(makerid,createdon,homakerid) values(@Uploader,@Date,@Id)

Refer this
http://technet.microsoft.com/en-us/library/ms190315.aspx
http://www.c-sharpcorner.com/UploadFile/rohatash/identity-and-scope_identity-in-sql-server-2012/
 
Share this answer
 
v3
Comments
arbaaz jalil 19-Aug-13 13:35pm    
What do i owe you my friend?
pradiprenushe 19-Aug-13 13:41pm    
Thanks for accepting answer. It is pleasure for me that answer is useful for you.
arbaaz jalil 19-Aug-13 13:43pm    
I mean .. how can i pay you back?
pradiprenushe 19-Aug-13 13:45pm    
Not needed its pleasure for me also that you got solution from me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900