How to insert and update data in a stored procedure

Source: Internet
Author: User

Stored procedures are very powerful. To some extent, they can even replace the business logic layer. Next, we will illustrate using stored procedures to insert or update statements.

1. Database Table Structure

The database used is SQL Server2008.

2. Create a stored procedure

(1) implementation functions:

    • Returns the same data directly (Return Value: 0 );
    • Update data with the same primary key but different data (Return Value: 2 );
    • Insert data for data processing (Return Value: 1 ).

Set the return value of a stored procedure based on different situations. When calling a stored procedure, perform related processing based on different return values.

(2) The following encoding is only a basic function. The specific SQL code is as follows:

 
 
  1. Create proc sp_Insert_Student
  2. @ No char (10 ),
  3. @ Name varchar (20 ),
  4. @ Sex char (2 ),
  5. @ Age int,
  6. @ Rtn int output
  7. As
  8. Declare
  9. @ TmpName varchar (20 ),
  10. @ TmpSex char (2 ),
  11. @ TmpAge int
  12. If exists (select * from Student where No = @ No)
  13. Begin
  14. Select @ tmpName = Name, @ tmpSex = Sex, @ tmpAge = Age from Student where No = @ No
  15. If (@ tmpName = @ Name) and (@ tmpSex = @ Sex) and (@ tmpAge = @ Age ))
  16. Begin
  17. Set @ rtn = 0 -- the same data is returned directly.
  18. End
  19. Else
  20. Begin
  21. Update Student set Name = @ Name, Sex = @ Sex, Age = @ Age where No = @ No
  22. Set @ rtn = 2 -- update data with the same primary key
  23. End
  24. End
  25. Else
  26. Begin
  27. Insert into Student values (@ No, @ Name, @ Sex, @ Age)
  28. Set @ rtn = 1 -- Insert the same data
  29. End
3. Call the Stored Procedure

In the SQL Server environment, the call is implemented easily.

The specific code is as follows:

 
 
  1. Declare @ rtn int
  2. Exec sp_Insert_Student '000000', 'zhang san', 'mal', 23, @ rtn output
  3.  
  4. If @ rtn = 0
  5. Print 'already exists. '
  6. Else if @ rtn = 1
  7. Print 'insert successful. '
  8. Else
  9. Print 'updated successfully'

A Stored Procedure achieves 3 conditions, which are highly efficient and flexible to use. Hope to help you.

In the process of growing up and learning, I will continue to share some of my experiences with you.

Edit recommendations]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.