用户定义的表类型中的外键

时间:2013-06-17 06:20:15

标签: c# sql-server tsql

您好我有一个用户定义的表格类型

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约束冲突........  问题是Target表有外键约束而用户定义表不是。我该如何解决这个问题?谢谢

执行合并的商店程序

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

0 个答案:

没有答案