SQL Server:IF EXIST在类似的数据库表之间更新或插入

时间:2016-09-27 09:26:21

标签: sql sql-server database sql-server-2008

我想使用一个简单的代码来更新或插入两个类似数据库之间的记录。

A 数据库中有一个如下表格:

CREATE TABLE [dbo].A.[tblDuyurular]
(
        [ID] [int] IDENTITY(1,1) NOT NULL,
        [SiteID] [int] NOT NULL,
        [Dil] [varchar](7) NULL,
        [DuyuruBaslik] [varchar](250) NOT NULL,
        [DuyuruIcerik] [varchar](max) NOT NULL
)

B 数据库中:

CREATE TABLE [dbo].B.[TBLDuyurular]
(
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [fakulteID] [int] NOT NULL,
    [DuyuruBaslik] [varchar](250) NOT NULL,
    [DuyuruIcerik] [varchar](max) NOT NULL,
    [dil] [varchar](7) NULL
)

匹配列是:

ID = ID,
SiteID = fakulteID,
Dil = dil,
DuyuruBaslik = DuyuruBaslik,
DuyuruIcerik = DuyuruIcerik,

我想查看dbo.B.TBLDuyurular记录。如果记录存在于dbo.A.tblDuyurular中,则使用dbo.B.TBLDuyurular的结果更新dbo.A.tblDuyurular。如果dbo.A.tblDuyurular中不存在记录,则将dERT.B.TBLDuyurular的结果插入到dbo.A.tblDuyurular中。表列最小化。这些表中的列太多了。

这种情况的最佳方式是什么?

请帮助。感谢。

2 个答案:

答案 0 :(得分:2)

尝试MERGE;

MERGE [dbo].A.[tblDuyuyular] AS TARGET
USING [dbo].B.[tblDuyuyular] AS SOURCE
ON TARGET.ID = SOURCE.ID 
AND TARGET.SiteID  = SOURCE.fakulteID 
AND TARGET.Dil  = SOURCE.dil
AND TARGET.DuyuruBaslik  = SOURCE.DuyuruBaslik 
AND TARGET.DuyuruIcerik  = SOURCE.DuyuruIcerik 
WHEN MATCHED
THEN UPDATE TARGET.SiteID  = SOURCE.fakulteID, TARGET.Dil  = SOURCE.dil, TARGET.DuyuruBaslik  = SOURCE.DuyuruBaslik, TARGET.DuyuruIcerik  = SOURCE.DuyuruIcerik -- Add Other Columns
WHEN NOT MATCHED
THEN INSERT(TARGET.SiteID, TARGET.Dil, TARGET.DuyuruBaslik, TARGET.DuyuruIcerik) VALUES(SOURCE.fakulteID, SOURCE.dil, SOURCE.DuyuruBaslik, SOURCE.DuyuruIcerik) -- Add Other Columns

如果你想在SOURCE中保持你的不匹配ID与TARGET表一样,那就按照这个;

SET IDENTITY_INSERT [dbo].A.[tblDuyuyular] ON;
GO

MERGE [dbo].A.[tblDuyuyular] AS TARGET
USING [dbo].B.[tblDuyuyular] AS SOURCE
ON TARGET.ID = SOURCE.ID 
AND TARGET.SiteID  = SOURCE.fakulteID 
AND TARGET.Dil  = SOURCE.dil
AND TARGET.DuyuruBaslik  = SOURCE.DuyuruBaslik 
AND TARGET.DuyuruIcerik  = SOURCE.DuyuruIcerik 
WHEN MATCHED
THEN UPDATE TARGET.SiteID  = SOURCE.fakulteID, TARGET.Dil  = SOURCE.dil, TARGET.DuyuruBaslik  = SOURCE.DuyuruBaslik, TARGET.DuyuruIcerik  = SOURCE.DuyuruIcerik -- Add Other Columns
WHEN NOT MATCHED
THEN INSERT(TARGET.ID, TARGET.SiteID, TARGET.Dil, TARGET.DuyuruBaslik, TARGET.DuyuruIcerik) VALUES(SOURCE.ID, SOURCE.fakulteID, SOURCE.dil, SOURCE.DuyuruBaslik, SOURCE.DuyuruIcerik); -- Add Other Columns

SET IDENTITY_INSERT [dbo].A.[tblDuyuyular] OFF;
GO

对于任何条件,更改第二行如下

USING 
(
SELECT * FROM [dbo].B.[tblDuyuyular] WHERE 'YOUR CONDITION' = 'YOUR CONDITION'
)
AS SOURCE

答案 1 :(得分:1)

您可以尝试合并声明

MERGE dbo.A.tblDuyurular as target
using dbo.B.tblDuyurular as source ON 
  ON  target.ID = source.ID,
  and target.SiteID = source.fakulteID,
  and target.Dil = source.dil,
  and target.DuyuruBaslik = source.DuyuruBaslik,
  and target.DuyuruIcerik = source.DuyuruIcerik,
WHEN matched THEN Update
  SET target.yourfields = source.yourfields
WHEN NOT MATCHED BY TARGET THEN insert (yourfields) values(source.yourfields)