【发布时间】:2017-08-18 02:17:55
【问题描述】:
我有一个表值参数,我想加入,不只是一次,而是两次。一次获取一行,第二次获取父行。
我几乎可以肯定这可以用普通的桌子来完成。可以用表值参数来完成吗?
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 一次时,我对这个存储过程没有任何问题。但是我第二次尝试加入表值参数时,存储过程似乎不再起作用。
我没有收到错误,我尝试加入的每一列都显示为空,好像加入的行不再匹配。
【问题讨论】:
标签: sql-server sql-server-2008 tsql join table-valued-parameters