Stored Procedures to insert into a table

Please support our MS SQL advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2005
Posts: 27
Reputation: wchitamb is an unknown quantity at this point 
Solved Threads: 0
wchitamb wchitamb is offline Offline
Light Poster

Stored Procedures to insert into a table

 
0
  #1
21 Days Ago
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
  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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,215
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 573
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
0
  #2
21 Days Ago
This should do the trick:
  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:
  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:
  1. id Surname Initials SEX_CODE
  2. ----------- ---------- ---------- ----------
  3. 1000 Scott2 SAK F
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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