Add or Update stored procedure

Reply

Join Date: Jan 2008
Posts: 32
Reputation: eddy556 is an unknown quantity at this point 
Solved Threads: 0
eddy556 eddy556 is offline Offline
Light Poster

Add or Update stored procedure

 
0
  #1
Dec 2nd, 2008
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

  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]
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 268
Reputation: Traicey is an unknown quantity at this point 
Solved Threads: 19
Traicey's Avatar
Traicey Traicey is offline Offline
Posting Whiz in Training

Re: Add or Update stored procedure

 
0
  #2
Dec 3rd, 2008
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
Some people get so rich they lose all respect for humanity. That's how rich I want to be.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 32
Reputation: eddy556 is an unknown quantity at this point 
Solved Threads: 0
eddy556 eddy556 is offline Offline
Light Poster

Re: Add or Update stored procedure

 
0
  #3
Dec 3rd, 2008
Yes I know that, hence if it already exists it should do an update! Not a replace.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 268
Reputation: Traicey is an unknown quantity at this point 
Solved Threads: 19
Traicey's Avatar
Traicey Traicey is offline Offline
Posting Whiz in Training

Re: Add or Update stored procedure

 
1
  #4
Dec 3rd, 2008
Well on that case you need to have an else statement

  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
Some people get so rich they lose all respect for humanity. That's how rich I want to be.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 32
Reputation: eddy556 is an unknown quantity at this point 
Solved Threads: 0
eddy556 eddy556 is offline Offline
Light Poster

Re: Add or Update stored procedure

 
0
  #5
Dec 3rd, 2008
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
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC