简体   繁体   English

使用MERGE语句将记录更新/插入到表中

[英]Update / Insert records to a table with a MERGE Statement

I did this 3 years ago with DB2 but can't remember how. 我3年前曾用DB2做过此事,但不记得如何做。

All I want to do is Update/Insert a record into a table. 我要做的就是将记录更新/插入到表中。 Rather than test for its existence and changing my DML, I want to do this with a parameterized insert/update(merge) T-SQL statement. 我不想测试它的存在并更改我的DML,而是想通过参数化的insert / update(merge)T-SQL语句来做到这一点。 I believe the procedure compiler optimizer will make this the most efficient method. 我相信程序编译器优化器将使它成为最有效的方法。

USE [MY_DB]
GO

/****** Object:  Table [dbo].[map_locations]    Script Date: 10/11/2015 9:29:26 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[map_locations](
    [loc_min_lat] [varchar](5) NOT NULL,
    [loc_min_lng] [varchar](6) NOT NULL,
    [loc_id] [int] NULL,
    [center] [varchar](20) NOT NULL CONSTRAINT [DF__map_locs__call___5E4ADDA8]  DEFAULT (''),
 CONSTRAINT [PK__map_map_locs__79C80F94] PRIMARY KEY CLUSTERED 
(
    [map_locations_lat] ASC,
    [map_locations_lng] ASC,
    [center] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

Simply put, I want to be able to insert a record into the above table if it does not violate the PK but update it if the record (from a PK perspective) exists. 简而言之,我希望能够在不违反PK的情况下将记录插入上表,但是如果存在该记录(从PK角度来看),则可以对其进行更新。

I have been researching and all the MS SQL examples are for two table merges. 我一直在研究,所有MS SQL示例均用于两个表合并。 I am passing in a record via parameters. 我通过参数传递记录。

I am working in Delphi XE10 (not that that should matter) and the database is MS SQL 2012. 我正在Delphi XE10中工作(那没关系),数据库是MS SQL 2012。

Any help appreciate. 任何帮助表示赞赏。

When you say "rather than", I assume you're using Sql Server 2008 or later and are wanting to use its Merge command (apologies for the earlier version of this answer). 当您说“而不是”时,我假设您正在使用Sql Server 2008或更高版本,并且想要使用其Merge命令(对于此答案的早期版本表示歉意)。

Here's a sample TransactSql script (tested and working, but using my table structure) which assumes you know the PK in advance: 这是一个TransactSql脚本示例(已测试并可以运行,但使用我的表结构),它假定您事先知道PK:

declare @id int

select @id = 1

merge table1 as dest
using (values (@id, 'name1'))
    as source (id, name)
    on dest.id = @id
when matched then
    update 
    set name = source.name
when not matched then
    insert ( id, name)
    values ( source.id, source.name);

select * from table1

From a Delphi app, you'd want to write that as a parameterized query, or, better, a parameterized call to a stored proc on the server. 从Delphi应用程序中,您希望将其编写为参数化查询,或者更好地是对服务器上存储的proc的参数化调用。

These days, no Delphi-tagged q about Sql seems complete without a mention of Sql-Injection, but using a parameterized query should minimise the risk of that. 如今,没有提及Sql-Injection的德尔福标记的关于Sql的q似乎是完整的,但是使用参数化查询应该可以最大程度地降低这种风险。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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