Có cách nào để nhận cột trong thời gian thực, từ truy vấn chính và sử dụng nó trong truy vấn phụ không?Máy chủ SQL - sử dụng các cột từ truy vấn chính trong truy vấn con
Cái gì đó như thế này: (Sử dụng A.item trong subquery)
SELECT item1, *
FROM TableA A
INNER JOIN
(
select *
from TableB B
where A.item = B.item
) on A.x = B.x;
Ok, đây là điều thực:
Tôi cần phải sửa đổi truy vấn hiện nay. Nó đã làm việc trước đây, nhưng bây giờ cơ sở dữ liệu đã thay đổi, tôi cần phải thực hiện một số sửa đổi, thêm một số so sánh. Như bạn có thể thấy có rất nhiều JOINS, và một trong số chúng là một truy vấn phụ. Tôi cần thêm một sự so sánh từ một cột từ truy vấn chính (từ T0 bảng ví dụ) để subquery (như thế này: T6.UnionAll_Empresa = T0.UnionALl_Empresa
)
Select T0.UnionAll_Empresa,<STUFF>
from [UNION_ALL_BASES]..OINV T0 with (nolock)
inner join [UNION_ALL_BASES]..INV6 T1 with (nolock) on t0.DocEntry = t1.DocEntry and t0.UnionAll_Empresa = t1.UnionAll_Empresa
inner join
(
select
t1.CompanyID,
T2.CompanyDb,
t1.OurNumber,
T6.BankCode,
T6.BankName,
T3.[Description] Situation,
T1.[Status],
T5.Descrption nomeStatus,
T1.Origin,
T1.DocEntry,
T1.DocType,
T1.ControlKey,
T1.CardCode,
T4.[Description] ContractBank,
T1.PayMethodCode,
T1.DueDate,
T1.DocDate,
T1.InstallmentID,
T1.InstallmentValue,
T1.Correction,
T1.InterestContractural,
T1.FineContract,
T1.ValueAbatment,
T1.ValueDiscount,
T1.ValueFineLate,
T1.ValueInterestDaysOfLate,
T1.OtherIncreases,
T1.ValueInWords,
T1.ValueDocument,
T1.DigitalLine,
T1.Document
from [IntegrationBank]..BillOfExchange T1 with (nolock)
inner join [InterCompany2]..CompanyHierarchy T2 with (nolock) on T1.CompanyID = T2.ID
left join [IntegrationBank]..BillOfExchangeSituation T3 with (nolock) on T1.Situation = T3.ID
inner join [IntegrationBank]..ContractBank T4 with (nolock) on T1.ContractBank = T4.ID
inner join [IntegrationBank]..BoeStatus T5 with (nolock) on T1.[Status] = T5.ID
inner join [UNION_ALL_BASES]..ODSC T6 with (nolock) on T4.BankKey = T6.AbsEntry and **T6.UnionAll_Empresa = T0.UnionALl_Empresa** --I need to do this
where T1.[Status] <> 5
and T2.CompanyDb = **T0.UnionAll_Empresa** --I need to do this
) TBI on (T1.DocEntry = TBI.DocEntry and T1.InstlmntID = TBI.InstallmentID and TBI.DocType = T1.ObjType)
inner join [UNION_ALL_BASES]..OCTG T2 on T0.GroupNum = T2.GroupNum and T0.UnionAll_Empresa = T2.UnionAll_Empresa
inner join [UNION_ALL_BASES]..OSLP T3 on T0.SlpCode = T3.SlpCode and T0.UnionAll_Empresa = T3.UnionAll_Empresa
where not exists (select 1
from [UNION_ALL_BASES]..RIN1 A with (nolock)
inner join [UNION_ALL_BASES]..ORIN B with (nolock) on A.DocEntry = B.DocEntry and A.UnionAll_Empresa = B.UnionAll_Empresa
where A.BaseEntry = T0.DocEntry
and B.SeqCode = ''1'')
bạn có thể chỉ định ý nghĩa của từ "nhận một trường trong thời gian thực" không? Thời gian thực? Điều đó nghĩa là gì? – ControlAltDel
Bạn không thể làm điều này vì đây không phải là truy vấn phụ. Đây là bảng có nguồn gốc. Tùy thuộc vào cơ sở dữ liệu của bạn, bạn có thể làm điều đó. MS Sql Server có 'cross apply/outer apply' cho mục đích này. Quan trọng hơn, tại sao bạn cần nó? Tại sao tham gia không phù hợp? –
Tôi đã cập nhật câu trả lời cho mã thực của mình. Bạn chỉ có thể thêm một điều kiện khác vào 'JOIN' của bạn và được thực hiện. – JNK