简体   繁体   中英

INSERT IGNORE in MS SQL Server 2005?

在MS SQL Server 2005中是否存在mysql的INSERT IGNORE等价物?

I'm not familiar with the mysql INSERT IGNORE feature, but the documentation I am reading makes it sound like all it does is ignore(silently fail) attempts to insert a record with a primary key that already exists in the table.

If that is what you are looking for, SQL Server has a similar functionality, but you have to apply it to the primary key or unique constraint when you create it.

Read about the IGNORE_DUP_KEY option of a unique index.

you can implement something like this with an INSTEAD OF trigger , intercepting the default insert action to perform updates where existing rows are detected. Not quite as out-of-the-box as the MySql variation, but more flexible.

I would say you have two viable options:

One is to filter out the wrong values before you try. Maybe add a where clause like:

WHERE newValues NOT IN (SELECT pk_col FROM yourTable)

OR you can use try-catch because whenever you work with errors, normally something with try-catch is your best friend. Try this out:

BEGIN TRY
    INSERT INTO yourTable(pk_col,other_date)
    SELECT 'PK Value That Already Exists','saflaksdfj'
END TRY

BEGIN CATCH
    --You can choose to do anything in response to an error like nothing!
END CATCH

我不知道你的申请是什么 - 也许REPLACE可能有帮助吗?!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM