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;