Insert_Update Stored Procedure and returning Identity

Please support our MS SQL advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2007
Posts: 33
Reputation: ansari.wajid is an unknown quantity at this point 
Solved Threads: 0
ansari.wajid ansari.wajid is offline Offline
Light Poster

Insert_Update Stored Procedure and returning Identity

 
0
  #1
Nov 6th, 2008
Hi All

I am writting an stored procedure to get the recently inserted records primary key as follows:


  1. ALTER PROCEDURE [DBO].[AddEditUserMsgDetails]
  2. (
  3. @iJobID [INT]=NULL,
  4. @iUserID [INT]=NULL,
  5. @vFromName [VARCHAR](50)=NULL,
  6. @vFromAddress [VARCHAR](50)=NULL,
  7. @vSubject [VARCHAR](50)=NULL,
  8. @iListID [INT]=NULL,
  9. @vActualText [VARCHAR](100)=NULL
  10. )
  11. AS
  12. BEGIN
  13. /*
  14.   Logic Comments: INSERTING OR UPDATING INTO THE TABLE ezlMailJobs
  15.   EXEC AddEditUserMsgDetails null,505,'waa','waa@v-empower.com','Test',816,
  16.   '<html><body>Hi &lt;!FN&gt;<br>please ignore</body></html></body></html>'
  17.  
  18. */
  19.  
  20. IF @iJobId IS NULL OR @iJobId=0
  21. BEGIN
  22. INSERT INTO ezlMailJobs (iUserID,vFromName,vFromAddress,vSubject,iListID,vActualText)
  23. VALUES (@iUserID,@vFromName,@vFromAddress,@vSubject,@iListID,@vActualText)
  24. SET @iJobID= @@Identity
  25. SELECT @iJobID
  26. END
  27. IF @iJobId IS NOT NULL
  28. BEGIN
  29. UPDATE ezlMailJobs SET iUserID=@iUserID,vFromName=@vFromName,vFromAddress=@vFromAddress,
  30. vSubject=@vSubject,iListID=@iListID,vActualText=@vActualText
  31. WHERE iJobId=@iJobId
  32. END
  33. IF @@ERROR=0
  34. BEGIN
  35. COMMIT TRAN
  36. SELECT 1
  37. END
  38. ELSE
  39. BEGIN
  40. ROLLBACK TRAN
  41. SELECT -1
  42. END


As in the above stored procedure when ijobid is null or zero, It should execute insert statement and return recently inserted records ijobid else it should execute update statement. But as now it is executing insert statement and returning some garbage value. one more thing I have declared ijobid as identity.
I am unable to figure out the problem. Please help me.
Thanks
WAA
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,162
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 137
dickersonka dickersonka is offline Offline
Veteran Poster

Re: Insert_Update Stored Procedure and returning Identity

 
0
  #2
Nov 6th, 2008
first you are missing an else on your first if checking jobid

after the insert use,
  1. RETURN @@IDENTITY

from codeside add a parameter with
  1. ParameterDirection.ReturnValue;
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the MS SQL Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC