外部应用 returns 行太多的 SPROC
SPROC with Outer-Apply returns too many rows
我有一个足够复杂的 SPROC,我将其修改为 return 多列(来自额外的 tables)。
我想要的是让此 SPROC 仍然具有 return 相同数量的行和相同的数据,但也为这些行填充新列(如果数据存在)。
我的修改确实填充了新列,但也填充了 returns 行(基于它找到的额外 tables 的值)。
这是附加 table 的示例:
Table 前锋:
FCBID
InvoiceID
FCBIDCode
CBCode
IsSet
FCBJrnID
ABC1
718
abcC1
cb1
0
1234
ABC2
718
abcC2
cb1
0
1234
ABC3
718
abcC3
cb1
0
1234
DEF1
718
abcC4
cb1
0
1234
DEF2
718
abcC5
cb1
0
1234
DEF3
718
abcC6
cb1
0
1234
BBB2
334
abcC7
cb2
1
3333
AAA5
225
abcC8
cb3
0
4444
Table 发票:
InvoiceID
ProdID
TrackNum
334
P4
T7
718
P1
T1
718
P2
T1
225
P5
T5
225
P6
T6
718
P3
T1
718
P9
T1
718
P3
T2
718
P9
T2
718
P1
T2
718
P2
T2
Table FCBAP:
FCBID
ProdID
TrackNum
ABC1
P1
T1
ABC2
P2
T1
ABC3
P3
T1
DEF1
P1
T2
DEF2
P2
T2
DEF3
P3
T2
BBB2
P4
T3
原始 SPROC(它的主要部分)围绕 InvoiceID 和 ProdID,当我执行原始 SPROC 时,它 return 只给我 2 行(正确行为)。
IE。按 InvoiceID = 718
搜索
InvoiceID
ProdID
718
P1
718
P2
718
P3
718
P9
我已将以下部分添加到此存储过程中:
在主查询中添加了列:
[FCBI].[FCBID],
[FCBI].[FCBIDCode],
[FCBI].[CBCode],
并且我添加了另一个外部应用部分,以从其他 tables:
中获取数据
OUTER APPLY
(
SELECT
[FCB].[FCBID],
[FCB].[FCBIDCode],
[LTC].[FieldText] AS [CBCode]
FROM [dbo].[FCB] AS [FCB]
INNER JOIN [dbo].[GenericCodes] AS [GRC] ON [GRC].[Guid] = [FCB].[CBCode]
CROSS APPLY [dbo].[GetTranslationTable] (@LanguageCode , [GRC].[DescriptionID]) AS [LTC]
WHERE
[FCB].[InvoiceID] = [MT].[InvoiceID]
AND FCB.IsSet = 0
AND FCB.FCBJrnID = MT.FCBJrnID
) AS [FCBI];
我希望在这种情况下 returned:
InvoiceID
ProdID
FCBID
FCBIDCode
CBCode
718
P1
ABC1
abcC1
cb1
718
P2
ABC2
abcC2
cb1
718
P3
ABC3
abcC3
cb1
718
P9
但我得到的是:
InvoiceID
ProdID
FCBID
FCBIDCode
CBCode
718
P1
ABC1
abcC1
cb1
718
P2
ABC2
abcC2
cb1
718
P3
ABC3
abcC3
cb1
718
P1
DEF1
abcC4
cb1
718
P2
DEF2
abcC5
cb1
718
P3
DEF3
abcC6
cb1
718
P9
不管它值多少钱,如果任何人稍后会清楚,解决方案是更改 OUTER APPLY 块,如下所示:
OUTER APPLY
(
SELECT
[FCB].[FCBID],
[FCB].[FCBIDCode],
[LTC].[FieldText] AS [CBCode]
FROM [dbo].[FCBAP] AS [FCBAP]
INNER JOIN [dbo].[FCB] AS [FCB] ON [FCB].[FCBID] = [FCBAP].[FCBID]
INNER JOIN [dbo].[GenericCodes] AS [GRC] ON [GRC].[Guid] = [FCB].[CBCode]
CROSS APPLY [dbo].[GetTranslationTable] (@LanguageCode , [GRC].[DescriptionID]) AS [LTC]
WHERE
[FCBAP].[ProdID] = [MT].[ProdID]
AND [FCBAP].[TrackNumb] = [MT].[TrackNum]
[FCB].[InvoiceID] = [MT].[InvoiceID]
AND FCB.IsSet = 0
AND FCB.FCBJrnID = MT.FCBJrnID
) AS [FCBI];
我有一个足够复杂的 SPROC,我将其修改为 return 多列(来自额外的 tables)。
我想要的是让此 SPROC 仍然具有 return 相同数量的行和相同的数据,但也为这些行填充新列(如果数据存在)。 我的修改确实填充了新列,但也填充了 returns 行(基于它找到的额外 tables 的值)。
这是附加 table 的示例:
Table 前锋:
FCBID | InvoiceID | FCBIDCode | CBCode | IsSet | FCBJrnID |
---|---|---|---|---|---|
ABC1 | 718 | abcC1 | cb1 | 0 | 1234 |
ABC2 | 718 | abcC2 | cb1 | 0 | 1234 |
ABC3 | 718 | abcC3 | cb1 | 0 | 1234 |
DEF1 | 718 | abcC4 | cb1 | 0 | 1234 |
DEF2 | 718 | abcC5 | cb1 | 0 | 1234 |
DEF3 | 718 | abcC6 | cb1 | 0 | 1234 |
BBB2 | 334 | abcC7 | cb2 | 1 | 3333 |
AAA5 | 225 | abcC8 | cb3 | 0 | 4444 |
Table 发票:
InvoiceID | ProdID | TrackNum |
---|---|---|
334 | P4 | T7 |
718 | P1 | T1 |
718 | P2 | T1 |
225 | P5 | T5 |
225 | P6 | T6 |
718 | P3 | T1 |
718 | P9 | T1 |
718 | P3 | T2 |
718 | P9 | T2 |
718 | P1 | T2 |
718 | P2 | T2 |
Table FCBAP:
FCBID | ProdID | TrackNum |
---|---|---|
ABC1 | P1 | T1 |
ABC2 | P2 | T1 |
ABC3 | P3 | T1 |
DEF1 | P1 | T2 |
DEF2 | P2 | T2 |
DEF3 | P3 | T2 |
BBB2 | P4 | T3 |
原始 SPROC(它的主要部分)围绕 InvoiceID 和 ProdID,当我执行原始 SPROC 时,它 return 只给我 2 行(正确行为)。 IE。按 InvoiceID = 718
搜索InvoiceID | ProdID |
---|---|
718 | P1 |
718 | P2 |
718 | P3 |
718 | P9 |
我已将以下部分添加到此存储过程中:
在主查询中添加了列:
[FCBI].[FCBID],
[FCBI].[FCBIDCode],
[FCBI].[CBCode],
并且我添加了另一个外部应用部分,以从其他 tables:
中获取数据OUTER APPLY
(
SELECT
[FCB].[FCBID],
[FCB].[FCBIDCode],
[LTC].[FieldText] AS [CBCode]
FROM [dbo].[FCB] AS [FCB]
INNER JOIN [dbo].[GenericCodes] AS [GRC] ON [GRC].[Guid] = [FCB].[CBCode]
CROSS APPLY [dbo].[GetTranslationTable] (@LanguageCode , [GRC].[DescriptionID]) AS [LTC]
WHERE
[FCB].[InvoiceID] = [MT].[InvoiceID]
AND FCB.IsSet = 0
AND FCB.FCBJrnID = MT.FCBJrnID
) AS [FCBI];
我希望在这种情况下 returned:
InvoiceID | ProdID | FCBID | FCBIDCode | CBCode |
---|---|---|---|---|
718 | P1 | ABC1 | abcC1 | cb1 |
718 | P2 | ABC2 | abcC2 | cb1 |
718 | P3 | ABC3 | abcC3 | cb1 |
718 | P9 |
但我得到的是:
InvoiceID | ProdID | FCBID | FCBIDCode | CBCode |
---|---|---|---|---|
718 | P1 | ABC1 | abcC1 | cb1 |
718 | P2 | ABC2 | abcC2 | cb1 |
718 | P3 | ABC3 | abcC3 | cb1 |
718 | P1 | DEF1 | abcC4 | cb1 |
718 | P2 | DEF2 | abcC5 | cb1 |
718 | P3 | DEF3 | abcC6 | cb1 |
718 | P9 |
不管它值多少钱,如果任何人稍后会清楚,解决方案是更改 OUTER APPLY 块,如下所示:
OUTER APPLY
(
SELECT
[FCB].[FCBID],
[FCB].[FCBIDCode],
[LTC].[FieldText] AS [CBCode]
FROM [dbo].[FCBAP] AS [FCBAP]
INNER JOIN [dbo].[FCB] AS [FCB] ON [FCB].[FCBID] = [FCBAP].[FCBID]
INNER JOIN [dbo].[GenericCodes] AS [GRC] ON [GRC].[Guid] = [FCB].[CBCode]
CROSS APPLY [dbo].[GetTranslationTable] (@LanguageCode , [GRC].[DescriptionID]) AS [LTC]
WHERE
[FCBAP].[ProdID] = [MT].[ProdID]
AND [FCBAP].[TrackNumb] = [MT].[TrackNum]
[FCB].[InvoiceID] = [MT].[InvoiceID]
AND FCB.IsSet = 0
AND FCB.FCBJrnID = MT.FCBJrnID
) AS [FCBI];