我正在尝试使用对存储过程的一次调用将多条记录插入到多个表中,该存储过程然后调用插入到各个表中的相应存储过程。
我正在使用 Insight.Database,它可以选择生成表值数据类型和将这些表值作为参数的存储过程。因此,我想我会创建自己的过程,它需要多个 TVP,并从这个 SP 中调用生成的 Insight.Database 过程:
CREATE PROCEDURE [dbo].[usp_FOBO_InsertMany]
(
@CLIPF [dbo].[CLIPFTable] READONLY,
@CLFPF [dbo].[CLFPFTable] READONLY,
@POLBAPF [dbo].[POLBAPFTable] READONLY,
@POL1PF [dbo].[POL1PFTable] READONLY,
@POL2PF [dbo].[POL2PFTable] READONLY,
@POL3PF [dbo].[POL3PFTable] READONLY,
@POL4PF [dbo].[POL4PFTable] READONLY,
@POL5PF [dbo].[POL5PFTable] READONLY,
@POL6PF [dbo].[POL6PFTable] READONLY,
@POL7PF [dbo].[POL7PFTable] READONLY,
@POLBPF [dbo].[POLBPFTable] READONLY,
@POLGPF [dbo].[POLGPFTable] READONLY,
@POLQPF [dbo].[POLQPFTable] READONLY,
@POLSIPF [dbo].[POLSIPFTable] READONLY
)
AS
EXEC usp_CLIPF_InsertMany @CLIPF
EXEC usp_CLFPF_InsertMany @CLFPF
EXEC usp_POLBAPF_InsertMany @POLBAPF
EXEC usp_POL1PF_InsertMany @POL1PF
EXEC usp_POL2PF_InsertMany @POL2PF
EXEC usp_POL3PF_InsertMany @POL3PF
EXEC usp_POL4PF_InsertMany @POL4PF
EXEC usp_POL5PF_InsertMany @POL5PF
EXEC usp_POL6PF_InsertMany @POL6PF
EXEC usp_POL7PF_InsertMany @POL7PF
EXEC usp_POLBPF_InsertMany @POLBPF
EXEC usp_POLGPF_InsertMany @POLGPF
EXEC usp_POLQPF_InsertMany @POLQPF
EXEC usp_POLSIPF_InsertMany @POLSIPF
GO
调用此过程的代码如下:
public Task<Int32> Migrate(IEnumerable<ProposalRow> proposals, String source, Func<Guid, Package> packageProvider)
{
List<CLIPFRow> clipf = new List<CLIPFRow>();
List<CLFPFRow> clfpf = new List<CLFPFRow>();
List<POLBAPFRow> polbapf = new List<POLBAPFRow>();
List<POL1PFRow> pol1pf = new List<POL1PFRow>();
List<POL2PFRow> pol2pf = new List<POL2PFRow>();
List<POL3PFRow> pol3pf = new List<POL3PFRow>();
List<POL4PFRow> pol4pf = new List<POL4PFRow>();
List<POL5PFRow> pol5pf = new List<POL5PFRow>();
List<POL6PFRow> pol6pf = new List<POL6PFRow>();
List<POL7PFRow> pol7pf = new List<POL7PFRow>();
List<POLBPFRow> polbpf = new List<POLBPFRow>();
List<POLGPFRow> polgpf = new List<POLGPFRow>();
List<POLQPFRow> polqpf = new List<POLQPFRow>();
List<POLSIPFRow> polsipf = new List<POLSIPFRow>();
foreach (var proposal in proposals)
{
clipf.AddRange(this.MapCLIPF(proposal, source));
pol1pf.Add(POL1PFRow.From(proposal, source));
polbapf.AddRange(POLBAPFRow.From(proposal, source));
pol2pf.AddRange(POL2PFRow.From(proposal, source));
pol3pf.AddRange(POL3PFRow.From(proposal, source));
pol4pf.AddRange(POL4PFRow.From(proposal, source));
pol5pf.AddRange(POL5PFRow.From(proposal, source));
pol6pf.AddRange(POL6PFRow.From(proposal, source));
if (proposal.Smi.StrategyPlans != null && proposal.Smi.StrategyPlans.Any())
{
pol7pf.AddRange(POL7PFRow.From(proposal, source));
}
if (proposal.Smi.Beneficiary != null)
{
polbpf.Add(POLBPFRow.From(proposal, source));
}
polgpf.AddRange(POLGPFRow.From(proposal, source));
if (proposal.Smi.Questionnaire != null && proposal.Smi.Questionnaire.Client != null)
{
polqpf.AddRange(POLQPFRow.From(proposal, source, packageProvider));
}
if (proposal.Smi.ProductMappingResult.BackOfficeProduct.BackOfficeProductCode.Equals("PUN4", StringComparison.InvariantCultureIgnoreCase))
{
polsipf.Add(POLSIPFRow.From(proposal, source));
}
clfpf.AddRange(CLFPFRow.From(proposal, source));
}
return base.DB.ExecuteAsync("usp_FOBO_InsertMany", new
{
CLIPF = clipf,
CLFPF = clfpf,
POLBAPF=polbapf,
POL1PF =pol1pf,
POL2PF =pol2pf,
POL3PF =pol3pf,
POL4PF =pol4pf,
POL5PF =pol5pf,
POL6PF =pol6pf,
POL7PF =pol7pf,
POLBPF =polbpf,
POLGPF =polgpf,
POLQPF =polqpf,
POLSIPF=polsipf
});
}
不幸的是,这给出了以下例外:
The incoming tabular data stream (TDS) remote procedure call (RPC) protocol stream is incorrect. Table-valued parameter 7 (""), row 1, column 8: Data type 0xE7 has an invalid data length or metadata length.
The data for table-valued parameter "@CLIPF" doesn't conform to the table type of the parameter. SQL Server error is: 8037, state: 30
The statement has been terminated.
有任何想法吗?