我有一个存储过程正在检查记录是否存在更新记录,如果不存在则应该插入记录。我正在使用Merge。更新部分正在运行,但插入部分不起作用。(名称和AID的组合是唯一的)。我的表中还有_IdxIdentity(自动增加)键。我在插入语句中没有使用它,我应该使用它吗?不知道如何使insert语句工作,它不会给我任何错误,只是不插入任何记录。
这是我的程序:
Create PROCEDURE [dbo].[insert_Or_Update_Agency]
@AID INT
,@_IsActive bit
,@Name varchar(40) = NULL
,@Description varchar(200)= NULL
,@ContactPhone varchar(50) = NULL
,@ContactName varchar(100)= NULL
,@ContactEmail varchar(100)= NULL
,@Country varchar(50) = NULL
,@Street varchar(100)= NULL
,@City varchar(100)= NULL
,@State varchar(50) = NULL
,@zipCode varchar(50) = NULL
AS
BEGIN
SET NOCOUNT ON;
Merge Agency USING
(Select * from Agency WHERE _IsActive = 1
and Name = @Name and AID=@AID) as Source
on Agency.Name= Source.Name and Agency.AID=Source.AID
when MATCHED
THEN Update
SET
_IsActive = COALESCE(@_IsActive,Source._IsActive)
,Name =COALESCE(@Name,Source.Name)
,[Description]=COALESCE(@Description,Source.Description)
,ContactPhone=COALESCE(@ContactPhone,Source.ContactPhone)
,ContactName=COALESCE(@ContactName,Source.ContactName)
,ContactEmail=COALESCE(@ContactEmail,Source.ContactEmail)
,Country=COALESCE(@Country,Source.Country)
,Street=COALESCE(@Street,Source.Street)
,City=COALESCE(@City,Source.City)
,[State]=COALESCE(@State,Source.State)
,zipCode=COALESCE(@zipCode,Source.zipCode)
WHEN NOT MATCHED
Then
Insert
(
AID
,_IsActive
,Name
,[Description]
,ContactPhone
,ContactName
,ContactEmail
,Country
,Street
,City
,[State]
,zipCode
)
VALUES
( @AID
,@_IsActive
,@Name
,@Description
,@ContactPhone
,@ContactName
,@ContactEmail
,@Country
,@Street
,@City
,@State
,@zipCode
);
END
这是代理商表:
CREATE TABLE [dbo].[Agency](
[_IdxIdentity] [bigint] IDENTITY(1,1) NOT NULL,
[_RowIdentity] [uniqueidentifier] NOT NULL,
[_DateTimeInserted] [datetime] NOT NULL,
[_DateTimeModified] [datetime] NOT NULL,
[_Partitioner] [int] NULL,
[_IsActive] [bit] NOT NULL,
[Name] [varchar](40) NULL,
[AID] [bigint] NULL
) ON [PRIMARY]
SET ANSI_PADDING ON
ALTER TABLE [dbo].[Agency] ADD [Description] [varchar](200) NULL
ALTER TABLE [dbo].[Agency] ADD [ContactPhone] [varchar](50) NULL
ALTER TABLE [dbo].[Agency] ADD [ContactName] [varchar](100) NULL
ALTER TABLE [dbo].[Agency] ADD [ContactEmail] [varchar](100) NULL
ALTER TABLE [dbo].[Agency] ADD [Country] [varchar](50) NULL
ALTER TABLE [dbo].[Agency] ADD [Street] [varchar](100) NULL
ALTER TABLE [dbo].[Agency] ADD [City] [varchar](100) NULL
ALTER TABLE [dbo].[Agency] ADD [State] [varchar](50) NULL
ALTER TABLE [dbo].[Agency] ADD [zipCode] [varchar](50) NULL
CONSTRAINT [PK_Agency] PRIMARY KEY CLUSTERED
(
[_IdxIdentity] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[Agency] ADD CONSTRAINT [DF_Agency__RowIdentity] DEFAULT (newid()) FOR [_RowIdentity]
GO
ALTER TABLE [dbo].[Agency] ADD CONSTRAINT [DF_Agency__DateTimeInserted] DEFAULT (getdate()) FOR [_DateTimeInserted]
GO
ALTER TABLE [dbo].[Agency] ADD CONSTRAINT [DF_Agency__DateTimeModified] DEFAULT (getdate()) FOR [_DateTimeModified]
GO
ALTER TABLE [dbo].[Agency] ADD CONSTRAINT [DF_Agency__IsActive] DEFAULT ((1)) FOR [_IsActive]
GO
答案 0 :(得分:0)
如果我正确地阅读了所有内容,我认为你需要从以下内容开始:
MERGE Agency
USING (SELECT @Name,@Aid) AS source (Name,Aid)
ON ( Agency.Name= Source.Name
AND Agency.AID=Source.AID
AND _IsActive = 1)
WHEN MATCHED THEN