我不知道我是否使用了正确的语法,但我试图进入1个表,然后是另一个具有外键的表。来自tbl1SentEmails
的主键用作tbl1Recipients
的外键。我已经把它放在一个存储过程中,但是,我一直收到一个说明外键约束的错误。有没有办法来解决这个问题?
存储过程代码:
-- This is the sample script to test the SP
-- An instance of the Table parameter type is created
Declare @SampelData As [dbo].[SampleDataType]
Declare @RecData As [dbo].[SampleRecType]
Insert Into @SampelData(strEmailSendID, dtmSent, strSubject, strBody, strConversationID, strConversationTopic, strConversationIndex, dtmReplied, dtmOpened, dtmClicked, blnTrackOpens, blnTrackClicks, blnTrackReplies, lngMergeID, blnHide)
Values ('ce4d29160fae4eba92c7d233723f6f59', '2016-11-08', 'strSubject', 'strBody','strConversationID', 'strConversationTopic', 'strConversationIndex', '2016-11-08', '2016-11-08', '2016-11-08', 'true', 'true', 'true', 69164, 'true')
SELECT * FROM @SampelData
Insert Into RecData(strEmailSendID, strEmailAddress, strDisplayName, strRecipientType)
Values ('ce4d29160fae4eba92c7d233723f6f59','dutta@standss.com.fj','Alvindra Dutt','TO')
Select * From @RecData
-- we then call the SP to store the values
Exec SampleProcedure @SampelData,@RecData
我将从MVC应用程序调用此存储过程,因此我只是为tbl1SentEmails
传递PK的原始值。但是,无论如何我可以获得传入表格的strEmailSendID
,然后将其输入tbl1Recipients
?
这是表类型的创建:
-- Create a table data type
CREATE TYPE [dbo].[SampleDataType] As Table
(
--This type has structure similar to the DB table
--SampleString Nvarchar(64) Not Null -- Having one String
--, SampleDate Date -- and one Date
strEmailSendID nvarchar(255) not null,
dtmSent datetime not null,
strSubject nvarchar(255) null,
strBody nvarchar(255) null,
strConversationID nvarchar(255) null,
strConversationTopic nvarchar(1000) null,
strConversationIndex nvarchar(max) null,
dtmReplied datetime null,
dtmOpened datetime null,
dtmClicked datetime null,
blnTrackOpens bit null,
blnTrackClicks bit null,
blnTrackReplies bit null,
lngMergeID bigint not null,
blnHide bit null,
lngLatestEventID bigint null,
strClickRespone nvarchar(50) null,
dtmClickResponse datetime null
)
CREATE TYPE [dbo].[SampleRecType] As Table
(
strEmailSendID nvarchar(255) not null,
strEmailAddress nvarchar(255) not null,
strDisplayName nvarchar(255) null,
strRecipientType nvarchar(5) not null
)
实际存储过程的代码:
--This is the Stored Procedure
CREATE PROCEDURE [dbo].[SampleProcedure]
(
-- which accepts one table value parameter.
-- It should be noted that the parameter is readonly
@Sample As [dbo].[SampleDataType] Readonly, @Rec As [dbo].[SampleRecType] ReadOnly
)
AS
Begin
-- We simply insert values into the DB table from the parameter
-- The table value parameter can be used like a table with only read rights
--Insert Into SampleTable(SampleString,SampleDate)
--Select SampleString,SampleDate From @Sample
Insert Into tbl1Recipients(strEmailSendID, strEmailAddress, strDisplayName, strRecipientType)
Select * From @Rec
Insert Into tbl1SentEmails(strEmailSendID, dtmSent, strSubject, strBody, strConversationID, strConversationTopic, strConversationIndex, dtmReplied, dtmOpened, dtmClicked, blnTrackOpens, blnTrackClicks, blnTrackReplies, lngMergeID, blnHide, lngLatestEventID, strClickResponse, dtmClickResponse)
Select * From @Sample
End
表格代码:
USE [standss_tracking_us_sandbox]
GO
/****** Object: Table [dbo].[tbl1Recipients] Script Date: 10/11/2016 5:24:52 p.m. ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[tbl1Recipients](
[strEmailSendID] [nvarchar](255) NOT NULL,
[lngRecipientNo] [bigint] IDENTITY(1,1) NOT NULL,
[strEmailAddress] [nvarchar](255) NOT NULL,
[strDisplayName] [nvarchar](255) NULL,
[strRecipientType] [nvarchar](5) NOT NULL,
[ts] [timestamp] NOT NULL,
CONSTRAINT [PK_tbl1Recipients] PRIMARY KEY CLUSTERED
(
[strEmailSendID] ASC,
[lngRecipientNo] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
)
GO
ALTER TABLE [dbo].[tbl1Recipients] WITH CHECK ADD CONSTRAINT [FK_tbl1Recipients_tbl1SentEmails] FOREIGN KEY([strEmailSendID])
REFERENCES [dbo].[tbl1SentEmails] ([strEmailSendID])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[tbl1Recipients] CHECK CONSTRAINT [FK_tbl1Recipients_tbl1SentEmails]
GO
/****** Object: Table [dbo].[tbl1SentEmails] Script Date: 10/11/2016 5:28:42 p.m. ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[tbl1SentEmails](
[strEmailSendID] [nvarchar](255) NOT NULL,
[dtmSent] [datetime] NOT NULL,
[strSubject] [nvarchar](255) NULL,
[strBody] [nvarchar](255) NULL,
[strConversationID] [nvarchar](255) NULL,
[strConversationTopic] [nvarchar](1000) NULL,
[strConversationIndex] [nvarchar](max) NULL,
[dtmReplied] [datetime] NULL,
[dtmOpened] [datetime] NULL,
[dtmClicked] [datetime] NULL,
[blnTrackOpens] [bit] NULL,
[blnTrackClicks] [bit] NULL,
[blnTrackReplies] [bit] NULL,
[ts] [timestamp] NOT NULL,
[lngMergeID] [bigint] NOT NULL,
[blnHide] [bit] NULL,
[lngLatestEventID] [bigint] NULL,
[strClickResponse] [nvarchar](50) NULL,
[dtmClickResponse] [datetime] NULL,
CONSTRAINT [PK_tbl1SentEmails] PRIMARY KEY CLUSTERED
(
[strEmailSendID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
)
GO
ALTER TABLE [dbo].[tbl1SentEmails] WITH CHECK ADD CONSTRAINT [FK_tbl1SentEmails_tbl1Merges] FOREIGN KEY([lngMergeID])
REFERENCES [dbo].[tbl1Merges] ([lngMergeID])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[tbl1SentEmails] CHECK CONSTRAINT [FK_tbl1SentEmails_tbl1Merges]
GO
答案 0 :(得分:1)
在插入tbl1Recipients
之前,看起来存储过程正在插入tbl1SentEmails
。 SQL Server does not support deferred foreign-key constraints因此您需要将该行添加到父表中,然后再将其添加到引用它的表中。
尝试切换存储过程中的INSERT
语句。
答案 1 :(得分:1)
如下所示更改存储过程。它适合你
ALTER PROCEDURE [dbo].[SampleProcedure]
(
@Sample As [dbo].[SampleDataType] Readonly,
@Rec As [dbo].[SampleRecType] ReadOnly
)
AS
Begin
Insert Into tbl1SentEmails(strEmailSendID, dtmSent, strSubject, strBody, strConversationID, strConversationTopic, strConversationIndex, dtmReplied, dtmOpened, dtmClicked, blnTrackOpens, blnTrackClicks, blnTrackReplies, lngMergeID, blnHide, lngLatestEventID, strClickResponse, dtmClickResponse)
Select * From @Sample
Insert Into tbl1Recipients(strEmailSendID, strEmailAddress, strDisplayName, strRecipientType)
Select * From @Rec
End