0
私は、テーブルに値を付けたいパラメータを1回だけではなく2回追加します。一度行を取得し、2回目は親行を取得します。T-SQLストアドプロシージャ結合テーブル値付きパラメータ2回
これは通常のテーブルで行うことができます。それはテーブル値のパラメータで行うことができますか?
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に一度だけ参加したとき、私はこのストアドプロシージャに問題はありませんでした。しかし、2回目にテーブル値のパラメータに参加しようとすると、ストアドプロシージャはもはや機能していないように見えます。
エラーが表示されません。結合しようとしているすべての列は、結合行がこれ以上一致しない場合のように、nullを表示しています。