【发布时间】:2017-03-24 00:05:14
【问题描述】:
我不知道我是否使用了正确的语法,但我正在尝试输入 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
【问题讨论】:
-
向我们展示创建表?并且不确定您在哪里得到错误。
-
@JuanCarlosOropeza,当我想插入
tbl1Recipients时,第二个INSERT语句出现错误。有一个名为strEmailSendID的字段是tbl1SentEmails的PK,并充当tbl1Recipients的FK -
您提供的语句仅包括插入到表变量中,而不包括插入到
tbl1Recipients或tbl1SentEmails中 -
为什么你的SP没有收到参数?
-
那么插入
tbl1Recipients和tbl1SentEmails发生在存储过程中?你能提供代码吗?
标签: sql-server stored-procedures