我有一个表值参数,我想加入,不只是一次,而是两次。一次得到一行,第二次得到父行。
我几乎可以肯定这可以用普通表来完成。可以用表值参数完成吗?
USE [ActivityStore_220.0_Model]
GO
/****** Object: StoredProcedure [dbo].[SnapshotLineItemAggregations] Script Date: 3/24/2017 5:49:43 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[SnapshotLineItemAggregations]
@billingCycleIteration int,
@tableOfBusinessEntities [dbo].[BusinessEntityNode] READONLY,
@tableOfActivityNames [dbo].[IdNamePair] READONLY
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
BEGIN TRANSACTION
--Note this will ONLY delete items that have the exact same BillingCycleIterationId.
--This will not delete old records from previous billing cycle iterations.
DELETE FROM [dbo].[LineItemAggregationSnapshot] WHERE
BillingCycleIterationId = @billingCycleIteration
-- Insert statements for procedure here
INSERT INTO [dbo].[LineItemAggregationSnapshot]
SELECT [BillingCycleIterationId]
,[BillToAccountId]
,CASE be_table.BusinessEntityTypeId WHEN 1 THEN 'Merchant' ELSE 'Organization' END AS [BusinessEntityType]
,[BusinessEntityId]
,[ProductId]
,[ActivityId]
,[RateProfileId]
,[Count]
,[Quantity]
,[ConversionRate]
,[OriginatingCurrency]
,[ConvertedQuantity]
,[QuantityUnits]
,[Rate]
,be_table.Name [BusinessEntityDescription]
,parent_be.Name [ParentBusinessEntityName]
,activity_table.Name [ItemDescription]
,[LineItemData]
FROM [dbo].[LineItemAggregation] lia
LEFT OUTER JOIN @tableOfBusinessEntities be_table
ON be_table.Id = lia.BusinessEntityId
LEFT OUTER JOIN @tableOfActivityNames activity_table
ON activity_table.Id = lia.ActivityId
LEFT OUTER JOIN @tableOfBusinessEntities parent_be --the second join
ON parent_be.Id = be_table.ParentId
WHERE BillingCycleIterationId = @billingCycleIteration
COMMIT TRANSACTION
select * from LineItemAggregationSnapshot
where BillingCycleIterationId = @billingCycleIteration
END
当我只加入@tableOfBusinessEntities时,我对这个存储过程没有任何问题。但是第二次尝试加入表值参数时,存储过程似乎不再起作用。
我没有收到错误,我尝试加入的每一列都显示为null,就好像连接行不再匹配一样。
答案 0 :(得分:1)
所以,错误在我的代码中的其他地方。我是如何填充.NET DataTable的,或者更确切地说我是不是。
一旦问题得到解决,那么存储过程就像魅力一样。
因此,对于将来想知道的人的答案是肯定的,您可以多次连接表值参数,就像在SQL Server中使用普通表一样。