SQL Server uses stored procedures to insert and update data. SQL Server Stored Procedures

Source: Internet
Author: User

SQL Server uses stored procedures to insert and update data. SQL Server Stored Procedures

Implementation

1) directly return the same data (Return Value: 0 );

2) update data with the same primary key but different data (Return Value: 2 );

3) insert data for data processing without data (Return Value: 1 ).

[Create a stored procedure]

Create proc Insert_Update @ Id varchar (20), @ Name varchar (20), @ Telephone varchar (20), @ Address varchar (20), @ Job varchar (20 ), @ returnValue int output as declare @ tmpName varchar (20), @ tmpTelephone varchar (20), @ tmpJob varchar (20), @ tmpAddress varchar (20) if exists (select * from dbo. demoData where id = @ Id) begin select @ tmpName = Name, @ tmpTelephone = Telephone, @ tmpAddress = Address, @ tmpJob = Job from dbo. demoData where id = @ Id if (@ tmpName = @ Name) and (@ tmpTelephone = @ Telephone) and (@ tmpAddress = @ Address) and (@ tmpJob = @ Job )) begin set @ returnValue = 0 -- returns the end else begin update dbo directly if the same data exists. demoData set Name = @ Name, Telephone = @ Telephone, Address = @ Address, Job = @ Job where id = @ Id set @ returnValue = 2 -- data with the same primary key, end else begin insert into dbo. demoData values (@ Id, @ Name, @ Telephone, @ Address, @ Job) set @ returnValue = 1 -- if there is no same data, insert it to the end.

[Execution method]

Declare @ returnValue int exec Insert_Update '15', 'hugh15', '000000', 'chang 'an Street ', 'vice Minister', @ returnValue output select @ returnValue

The return value is 0. The same already exists.

Return Value 1, insert successful

Return Value 2. Update successful


Insert data using SQL Server Stored Procedures

Where is the task not executed? Even if the stored procedure is not executed, is the execution interrupted at a certain point?
--------
What do you call "no execution" mean that the result is not obtained or the creation is successful?

How to insert multiple data records to a table at the same time using stored procedures in SQL Server

Write two INSERT statements in the stored procedure.
Create proc up_insert
As
Begin
Insert into test valuse (1 );
Insert into test values (2 );
End;

Related Article

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.