943,549 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Unsolved
  • Views: 4403
  • MS SQL RSS
Nov 6th, 2008
0

Insert_Update Stored Procedure and returning Identity

Expand Post »
Hi All

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


MS SQL Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Light Poster
ansari.wajid is offline Offline
47 posts
since Aug 2007
Nov 6th, 2008
0

Re: Insert_Update Stored Procedure and returning Identity

first you are missing an else on your first if checking jobid

after the insert use,
MS SQL Syntax (Toggle Plain Text)
  1. RETURN @@IDENTITY

from codeside add a parameter with
MS SQL Syntax (Toggle Plain Text)
  1. ParameterDirection.ReturnValue;
Reputation Points: 133
Solved Threads: 141
Veteran Poster
dickersonka is offline Offline
1,162 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: Autogenerated Primary Key.. Please Help !!
Next Thread in MS SQL Forum Timeline: Can't insert special characters to SQL





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC