943,944 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Marked Solved
  • Views: 3541
  • MS SQL RSS
Nov 10th, 2009
0

Stored Procedures to insert into a table

Expand Post »
I have some tables that i have created in SQL Server and i want to write a stored Procedure that will insert data on to one of my tables AND also be able to update say if i select record
MS SQL Syntax (Toggle Plain Text)
  1. CREATE PROCEDURE Sp_cust_SaveCustomer
  2. --ALTER PROCEDURE cust_SaveCustomer
  3. (@id int,
  4. @ Surname varchar(50),
  5. @ Initials varchar(50),
  6. @ SEX_CODE varchar(50))
  7. AS IF @id=-1
  8. BEGIN INSERT INTO cust_Customer(Surname,Initials,SEX_CODE)
  9. SELECT @ Surname,@ Initials,@SEX_CODE
  10. SELECT @@identity
  11. END
  12. ELSE
  13. BEGIN UPDATE cust_Customer
  14. SET
  15. Surname= @ Surname ,
  16. Initials =@ Initials ,
  17. SEX_CODE =@ SEX_CODE
  18. WHERE id=@id
  19. SELECT @id
  20. END
Reputation Points: 10
Solved Threads: 0
Light Poster
wchitamb is offline Offline
31 posts
since Jul 2005
Nov 10th, 2009
0
Re: Stored Procedures to insert into a table
This should do the trick:
MS SQL Syntax (Toggle Plain Text)
  1. IF OBJECT_ID('test_Customer', 'U') IS NOT NULL DROP TABLE test_Customer
  2. GO
  3. CREATE TABLE test_Customer
  4. (
  5. id int identity(1000, 1) PRIMARY KEY,
  6. Surname varchar(50),
  7. Initials varchar(50),
  8. SEX_CODE varchar(50)
  9. )
  10. GO
  11. IF OBJECT_ID('Sp_test_SaveCustomer', 'P') IS NOT NULL DROP PROCEDURE Sp_test_SaveCustomer
  12. GO
  13. CREATE PROCEDURE Sp_test_SaveCustomer
  14. (
  15. @id int,
  16. @Surname varchar(50),
  17. @Initials varchar(50),
  18. @SEX_CODE varchar(50)
  19. )
  20. AS
  21. BEGIN
  22.  
  23. Declare @RESULT int
  24. SET @RESULT = 0
  25.  
  26. IF (@id=-1)
  27. BEGIN
  28. INSERT INTO test_Customer(Surname,Initials,SEX_CODE) VALUES (@Surname, @Initials, @SEX_CODE)
  29. SET @RESULT = Cast(SCOPE_IDENTITY() AS int)
  30. END ELSE
  31. BEGIN
  32. UPDATE test_Customer
  33. SET
  34. Surname= @Surname,
  35. Initials =@Initials,
  36. SEX_CODE =@SEX_CODE
  37. WHERE id = @id
  38.  
  39. SET @RESULT = @id
  40. END
  41.  
  42. RETURN @RESULT
  43.  
  44. END
  45.  
  46.  
  47.  
  48. GO
  49.  
  50. Declare @identity int
  51. exec @identity = dbo.Sp_test_SaveCustomer -1, 'Scott', 'SAK', 'M'
  52. PRINT Cast(@identity AS varchar)
  53.  
  54. exec @identity = dbo.Sp_test_SaveCustomer 1000, 'Scott2', 'SAK', 'F'
  55. PRINT Cast(@identity AS varchar)
  56.  
  57. SELECT * FROM test_Customer

Results in:
text Syntax (Toggle Plain Text)
  1.  
  2. (1 row(s) affected)
  3. 1000
  4.  
  5. (1 row(s) affected)
  6. 1000
  7.  
  8. (1 row(s) affected)

And the final data:
text Syntax (Toggle Plain Text)
  1. id Surname Initials SEX_CODE
  2. ----------- ---------- ---------- ----------
  3. 1000 Scott2 SAK F
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: is there a way to make an image data type as index?
Next Thread in MS SQL Forum Timeline: Connect by prior command





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


Follow us on Twitter


© 2011 DaniWeb® LLC