SQL Server inserts updated data with stored procedure implementations

Source: Internet
Author: User

Realize

1) with the same data, return directly (return value: 0);

2) The data with the same primary key, but different data, to update processing (return value: 2);

3) No data, insert processing (return value: 1).


"Create 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 [email protected])

Begin

Select @tmpName =name, @tmpTelephone =telephone, @tmpAddress =address, @tmpJob =job from dbo. Demodata where [email protected]

if ((@[email protected]) and (@[email protected]) and (@[email protected]) and (@[email protected]))

Begin

Set @returnValue = 0--Has the same data, returns the value directly

End

Else

Begin

UPDATE dbo. Demodata Set [email protected],[email protected],[email protected],[email protected] where [email protected]

Set @returnValue = 2--with the same data as the primary key for update processing

End

End

Else

Begin

INSERT INTO dbo. Demodata values (@Id, @Name, @Telephone, @Address, @Job)

Set @returnValue = 1--no same data for insert processing

End


"Execution Mode"

DECLARE @returnValue int
exec insert_update ' hugh15 ', ' 3823345 ', ' Changan Street ', ' Deputy minister ', @returnValue output
Select @returnValue


The return value 0, already exists the same

Return value 1, insert successful

Return value 2, update successful


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.