글쎄, 시작하자.다중 테이블과 키가있는 내부 결합
동일한 데이터베이스를 가진 4 대의 서버가 있습니다. 각 DB에서 개별적으로 데이터를 가져 오는 쿼리가 있습니다. 다음과 같은 내용이 있습니다 :
SELECT "blablablablabla"
FROM [UNION_ALL_BASES]..OVPM T01 with (nolock)
INNER JOIN [UNION_ALL_BASES]..VPM2 T11 with (nolock) ON T01.UnionAll_Empresa = T11.UnionAll_Empresa and T01.DocEntry = T11.DocNum
INNER JOIN [UNION_ALL_BASES]..PCH6 T2 with (nolock) ON T11.UnionAll_Empresa = T2.UnionAll_Empresa and T11.DocEntry = T2.DocEntry and T11.InstId = T2.InstlmntID
INNER JOIN [UNION_ALL_BASES]..OPCH T3 with (nolock) ON T2.UnionAll_Empresa = T3.UnionAll_Empresa and T2.DocEntry = T3.DocEntry
INNER JOIN [UNION_ALL_BASES]..PCH1 T3a with (nolock) ON T3.UnionAll_Empresa = T3a.UnionAll_Empresa and T3.DocEntry = T3a.DocEntry
LEFT JOIN [UNION_ALL_BASES]..OOCR T3b with (nolock) ON T3a.UnionAll_Empresa = T3b.UnionAll_Empresa and T3a.OcrCode2 = T3b.OcrCode
INNER JOIN [UNION_ALL_BASES]..OSLP T4 with (nolock) ON T4.UnionAll_Empresa = T4.UnionAll_Empresa and T3.SlpCode = T4.SlpCode
and T11.InvType = ''18''
AND T01.Canceled <> ''Y''
AND T01.CashSum > 0
오늘 우리는이 4 가지 DB를 모두 병합하는 하나의 새로운 데이터베이스를 보유하고 있습니다. 이 새로운 DB의 각 테이블은 Union All을 가진 뷰이며, 이제는이 행이 어떤 서버에서 왔는지를 알기 위해 "UnionAll_Empresa"라는 새 열을 추가했습니다. 예를 들어
:
CREATE VIEW AACP as
SELECT 'G8Networks Solucoes' as UnionAll_Empresa, * from SBO_G8NETWORKS_SOLUCOES.dbo.AACP
UNION All
SELECT 'G8Networks NIC' as UnionAll_Empresa, * from SBO_G8NETWORKS_NIC.dbo.AACP
UNION All
SELECT 'SPIN' as UnionAll_Empresa, * from SBO_SPIN.dbo.AACP
UNION All
SELECT 'FA2R' as UnionAll_Empresa, * from SBO_FA2R.dbo.AACP;
지금 나는이 새로운 "테이블"로, 다시 기존 쿼리를 사용해야합니다. 문제는 동일한 테이블에 4 개의 서버가 있으므로 기본 키가 더 이상 고유하지 않다는 것입니다. 그래서 해결책은 "UnionAll_Empresa"필드 (서버의 이름을 알려주는)를 쿼리의 기본 키와 함께 키로 추가하는 것입니다. 새로운 쿼리는 다음과 같이 될 것이다 :
SELECT "blalalalalala"
FROM [UNION_ALL_BASES]..OVPM T01 with (nolock)
INNER JOIN [UNION_ALL_BASES]..VPM2 T11 with (nolock) ON T01.DocEntry = T11.DocNum and T01.UnionAll_Empresa = T11.UnionAll_Empresa
INNER JOIN [UNION_ALL_BASES]..PCH6 T2 with (nolock) ON T11.DocEntry = T2.DocEntry and T11.InstId = T2.InstlmntID and T11.UnionAll_Empresa = T2.UnionAll_Empresa
INNER JOIN [UNION_ALL_BASES]..OPCH T3 with (nolock) ON T2.DocEntry = T3.DocEntry and T2.UnionAll_Empresa = T3.UnionAll_Empresa
INNER JOIN [UNION_ALL_BASES]..PCH1 T3a with (nolock) ON T3.DocEntry = T3a.DocEntry and T3.UnionAll_Empresa = T3a.UnionAll_Empresa
LEFT JOIN [UNION_ALL_BASES]..OOCR T3b with (nolock) ON T3a.OcrCode2 = T3b.OcrCode and T3a.UnionAll_Empresa = T3b.UnionAll_Empresa
INNER JOIN [UNION_ALL_BASES]..OSLP T4 with (nolock) ON T3.SlpCode = T4.SlpCode and T4.UnionAll_Empresa = T4.UnionAll_Empresa
WHERE T11.InvType = ''18''
AND T01.Canceled <> ''Y''
AND T01.CashSum > 0
그러나 그것은 오류를 보여줍니다
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
그것은 내가 열 "UnionAll_Empresa와 비교를 추가 할 경우에도, 일부 기본 키에 대해 여러 결과를 찾을 스틸 사진 "(원래 서버의 이름).
내가 잘못 했나요?
--- 여기에 전체 쿼리
set @vQuery = '
SELECT --Pagametos de NF em dinheiro
T01.UnionAll_Empresa,
Base = ''' + @database + ''',
NomeEmp = (select a.CompnyName from [UNION_ALL_BASES]..OADM a with (nolock)),
CNPJ = (select a.TaxIdNum from [UNION_ALL_BASES]..oadm a with (nolock)),
IE = (Select TaxIdNum2 from [UNION_ALL_BASES]..OADM with (nolock)),
Filial = (Select PrintHeadr from [UNION_ALL_BASES]..OADM with (nolock)),
Proj = isnull(T3.Project,''''),
OcrCode2 = isnull(T3a.OcrCode2,''0''),
OcrName = isnull(T3b.OcrName,''Sem projeto definido''),
''NF'' TipoDoc,
''DIN'' Tipo,
T01.CardCode,
T01.CardName,
CardFName = (select CardFName from [UNION_ALL_BASES]..OCRD with (nolock) where OCRD.CardCode = T01.CardCode and OCRD.UnionAll_Empresa = T01.UnionAll_Empresa),
T01.DocEntry CodigoBaixaPagamento,
T11.DocEntry NumeroSapDocumento,
T01.DocDate DataDocBaixa,
T3.DocDate,
T11.InstId Parcela,
'''' SituacaoParc,
''1900-01-01'' DataUltBaixa,
T11.InvType,
T2.InsTotal ValorDaParcelaOriginal,
T01.DocDate DataLancamentoBaixa,
T2.DueDate VencimentoOriginalParcela,
T3.SeqCode,
SerieNF = (select SeqName from [UNION_ALL_BASES]..NFN1 with (nolock) where SeqCode = T3.SeqCode and UnionAll_Empresa = T3.UnionAll_Empresa),
T3.Series,
T3.Serial,
T3.SlpCode,
T4.SlpName,
round((T3a.LineTotal/T3.DocTotal)*(T11.SumApplied/T01.DocTotal)*(T01.CashSum),2) ValorPago,
T01.DocTotal TotalBaixa,
T01.CashSum TotalDinBaixa,
T01.TrsfrSum TotalTransfBaixa,
T01.[CheckSum] TotalCHBaixa,
T01.BoeSum TotalBoeBaixa,
T01.CreditSum TotalCCredBaixa,
Case
When T01.CashSum > 0 Then ''Dinheiro''
When T01.TrsFrSum > 0 Then ''Transferência''
When T01.CreditSum > 0 Then ''Cartao''
End TipoDocumento,
'''' NossoNumBol,
'''' DigNossoNumBol,
''1900-01-01'' VencBoleto,
'''' CodBancoBol,
'''' NomeBancoBol,
0 VlrTotBol,
'''' NomeFPagtoBol,
'''' LinhaDigBol,
'''' TrsfrRef,
'''' NumCH,
'''' DataCH,
'''' StatusCH,
0 VlrTotCH,
'''' BancoCH,
'''' AgenciaCH,
'''' ContaCH,
'''' BoeStatus,
'''' CodCCred,
'''' NomeCCred,
'''' NumComprCartao,
0 NumParcCartao,
''1900-01-01'' PrimVencimentoCartao,
0 VlrTotCartao,
VlrDin = round((T3a.LineTotal/T3.DocTotal)*(T11.SumApplied/T01.DocTotal) * T01.CashSum,2),
VlrTransf = 0,
VlrCredit = 0,
VlrBol = 0,
VlrCH = 0,
VlrCart = 0,
VlrDev = 0,
VlrBLI = 0
FROM [UNION_ALL_BASES]..OVPM T01 with (nolock)
INNER JOIN [UNION_ALL_BASES]..VPM2 T11 with (nolock) ON T01.DocEntry = T11.DocNum and T01.UnionAll_Empresa = T11.UnionAll_Empresa
INNER JOIN [UNION_ALL_BASES]..PCH6 T2 with (nolock) ON T11.DocEntry = T2.DocEntry and T11.InstId = T2.InstlmntID and T11.UnionAll_Empresa = T2.UnionAll_Empresa
INNER JOIN [UNION_ALL_BASES]..OPCH T3 with (nolock) ON T2.DocEntry = T3.DocEntry and T2.UnionAll_Empresa = T3.UnionAll_Empresa
INNER JOIN [UNION_ALL_BASES]..PCH1 T3a with (nolock) ON T3.DocEntry = T3a.DocEntry and T3.UnionAll_Empresa = T3a.UnionAll_Empresa
LEFT JOIN [UNION_ALL_BASES]..OOCR T3b with (nolock) ON T3a.OcrCode2 = T3b.OcrCode and T3a.UnionAll_Empresa = T3b.UnionAll_Empresa
INNER JOIN [UNION_ALL_BASES]..OSLP T4 with (nolock) ON T3.SlpCode = T4.SlpCode and T4.UnionAll_Empresa = T4.UnionAll_Empresa
WHERE T11.InvType = ''18''
AND T01.Canceled <> ''Y''
AND T01.CashSum > 0
'
exec ('insert into #RelContasPagar ' + @vQuery)
나열된 쿼리에 하위 쿼리가 표시되지 않습니다. 어딘가에 선택 목록에 하나 있어야합니다. "blabla ..."확장 해 주시겠습니까? –
있습니다. 문제가 조인 될 수 있다고 생각했습니다. 조인 또는 "where"절을 찾을 때마다 일치하지 않는 기본 키를 피하기 위해 "UnionAll_Empresa"comparsion을 추가하지만 무언가를 놓치고 있습니다. –