943,929 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Unsolved
  • Views: 5287
  • MS SQL RSS
Dec 2nd, 2008
0

Add or Update stored procedure

Expand Post »
The following stored procedure is supposed to add a new record to the database OR update Mdescript if it already exists. However I keep getting errors such as primary key violations. Please take a look, many thanks

SQL Syntax (Toggle Plain Text)
  1. USE [COSHH2008]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[CHSP_OR_ACTIVITYAREA_001] Script Date: 12/02/2008 13:59:33 ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8. ALTER PROCEDURE [dbo].[CHSP_OU_METHOD_001] @Method VARCHAR(50), @Mdescript VARCHAR(MAX)
  9.  
  10. AS
  11.  
  12. DECLARE @rc INTEGER
  13.  
  14. SET @rc =
  15.  
  16. ( SELECT @@ROWCOUNT
  17. FROM Method as a
  18. --
  19. WHERE a.Method = @Method AND a.Mdescript = @Mdescript)
  20.  
  21. IF @rc IS NULL
  22. BEGIN
  23.  
  24. --SET @rc = 0
  25.  
  26. INSERT INTO Method VALUES (@Method, @Mdescript)
  27.  
  28. END
  29.  
  30. DECLARE @mc INTEGER
  31.  
  32. SET @mc =
  33.  
  34. (SELECT @@ROWCOUNT
  35.  
  36. FROM Method as m
  37.  
  38. WHERE m.METHOD = @Method)
  39.  
  40. IF @mc = 1
  41. BEGIN
  42.  
  43. UPDATE Method
  44. SET Mdescript = @Mdescript
  45. WHERE Method = @Method
  46.  
  47.  
  48.  
  49. END
Last edited by peter_budo; Dec 3rd, 2008 at 7:16 pm. Reason: Correcting closing tag from [code] to [/code]
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
eddy556 is offline Offline
32 posts
since Jan 2008
Dec 3rd, 2008
0

Re: Add or Update stored procedure

Primary key violation usually means that U are trying to enter or insert a value that already exist in the primary key field, there is nothing wrong I see in the code, Primary keys have to be unique
Reputation Points: 26
Solved Threads: 19
Posting Whiz in Training
Traicey is offline Offline
283 posts
since Mar 2008
Dec 3rd, 2008
0

Re: Add or Update stored procedure

Yes I know that, hence if it already exists it should do an update! Not a replace.
Reputation Points: 10
Solved Threads: 0
Light Poster
eddy556 is offline Offline
32 posts
since Jan 2008
Dec 3rd, 2008
1

Re: Add or Update stored procedure

Well on that case you need to have an else statement

sql Syntax (Toggle Plain Text)
  1. IF @rc IS NULL
  2. BEGIN
  3. --SET @rc = 0
  4. INSERT INTO Method VALUES (@Method, @Mdescript)
  5. SELECT @@IDENTITY
  6. END
  7. ELSE
  8. UPDATE YOUR RECORD
  9. SELECT @RC
Reputation Points: 26
Solved Threads: 19
Posting Whiz in Training
Traicey is offline Offline
283 posts
since Mar 2008
Dec 3rd, 2008
0

Re: Add or Update stored procedure

Hi thanks for that, but it turned out the whole of the code can be simplified using IF EXISTS, arghh this is so obvious when I think about it. Sorry about the newbie post everyone
Reputation Points: 10
Solved Threads: 0
Light Poster
eddy556 is offline Offline
32 posts
since Jan 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: Error In trigger.
Next Thread in MS SQL Forum Timeline: Overflow full database to a new database





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


Follow us on Twitter


© 2011 DaniWeb® LLC