0

嗨,我有一个用户定义的表格类型

CREATE TYPE [dbo].[et] AS TABLE(
    [id] [int] IDENTITY(1,1) NOT NULL,
    [scId] [int] NOT NULL,
    [eN] [int] NOT NULL,
    [suppId] [int] NOT NULL,
)
GO

比我试图更新/插入到目标表的目标表是

CREATE TABLE [dbo].[test](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [scId] [int] NOT NULL,
    [eN] [int] NOT NULL,
    [suppId] [int] NOT NULL,
 CONSTRAINT [PK_test] PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[t_test]  WITH CHECK ADD  CONSTRAINT [FK_test__supplyr] FOREIGN KEY([suppId])
REFERENCES [dbo].[supplyr] ([id])
GO

当我使用我的代码传递表格时

  SqlParameter param=  cmdd.Parameters.Add(new SqlParameter("@mt", dt3));

我得到错误。MERGE 语句与 FOREIGN KEY 约束冲突......问题是目标表具有外键约束,而用户定义表没有。我该如何解决这个问题?谢谢

做合并的存储过程是

ALTER procedure [dbo].[testing2]
@mt[dbo].[et] readonly 
 as 
Begin
merge into [dbo].[test] as Target
using @mt as Source 
on Target.suppId =Source.suppId
when matched then 
update set target.eN=Source.eN
when not matched then 
insert (sCId,eN,suppId)
values (Source.sCId, Source.eN, Source.suppId);
End
4

0 回答 0