SQL MySQL is present on update, and there is no notation for inserting (go)

Source: Internet
Author: User
Tags rowcount

Transferred from: HTTP://HI.BAIDU.COM/TIDY0608/ITEM/FF930FE2436F2601560F1DD9

Sqlsever data exists on the update, there is no insertion of two methods

Two ways to use it frequently:

1. Update, if @ @ROWCOUNT = 0 Then insert

UPDATETable1 SETColumn1 = @newValue Whereid = @id

[email protected] @ROWCOUNT = 0

BEGIN

INSERT INTOTable1 (Id, Column1) VALUES (@id, @newValue)

END

If a row of data exists for the update to be valid, the insert will be executed. Personal feelings This approach tends to be the case where data is mostly present in the data table;

2. If row exists update, otherwise insert

Ifexists (select* FROMTable1 whereid = @id)

BEGIN

UPDATETable1 SETColumn1 = @newValue Whereid = @id

END

ELSE

BEGIN

INSERT INTOTable1 (Id, Column1) VALUES (@id, @newValue)

END

In this method, the SELECT statement executes immediately after execution of update or insert, and consumes less resources than method one.

3.mysql data exists on update, does not exist on insert

Insert into T (F1,F2,F3) VALUES (v1,v2,v3) on DUPLICATE KEY UPDATE v3=values (v3) +v3

SQL MySQL is present on update, and there is no notation for inserting (go)

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.