我有一个脚本:
SELECT DMC
,a.[id]
,a.[PSN]
,a.[Status]
,a.[CreationDate]
,a.[DBInsertDatetime]
,c.Wuchtvers_anz
,c.[MD1_ANLIEF_MG]
,c.[MD2_ANLIEF_G]
FROM
[tblTcInfo] a
inner join [dbo].[tblC] b on a.PSN=b.PSN
inner join [Daten] c on DMC=LGNR
where a.PSN in (select a.PSN from [tblTcInfo] a inner join [dbo].[tblC] b on a.PSN=b.PSN
where DMC in
(
'75044500718481203100--1802290018'
)
这给了我输出:
DMC id PSN Status CreationDate DBInsertDatetime Wuchtvers_anz MD1_ANLIEF_MG MD2_ANLIEF_G
00003064418481504503--1735304527 5861374 7500421056230120180527148491 1 2018-01-23 05:34:19.000 2018-01-23 05:34:19.680 2 7,72 169
00003064418481504503--1735304527 5861374 7500421056230120180527148491 1 2018-01-23 05:34:19.000 2018-01-23 05:34:19.680 2 7,72 169
我期望输出:
DMC id PSN Status CreationDate DBInsertDatetime Wuchtvers_anz MD1_ANLIEF_MG MD2_ANLIEF_G Input_column
00003064418481504503--1735304527 5861374 7500421056230120180527148491 1 2018-01-23 05:34:19.000 2018-01-23 05:34:19.680 2 7,72 169 75044500718481203100--1802290018
00003064418481504503--1735304527 5861374 7500421056230120180527148491 1 2018-01-23 05:34:19.000 2018-01-23 05:34:19.680 2 7,72 169 75044500718481203100--1802290018
不能使用以下解决方案:
'75044500718481203100--1802290018' as Input_column
因为我将在where子句中输入很多行。
我尝试将多个set
与declare
结合使用,但是仍然无法正常工作。我发现的另一个选择是,将declare
与insert
一起使用,但是在这里我应该声明每个变量。该[tblTcInfo]表很棘手! -我可以过滤
DMC
由ex。 2
并获得3
。不要问我为什么!就是这样。非常感谢您的帮助。
答案 0 :(得分:0)
您需要使用JOIN
重新定义逻辑,以便所需的列可用于外部查询。
这对您来说更困难,因为:
重组查询后,该列将可用:
select i.DMC, i.[id], i.[PSN], i.[Status], i.CreationDate, i.[DBInsertDatetime],
d.Wuchtvers_anz, d.MD1_ANLIEF_MG, d.MD2_ANLIEF_G,
cc.DMC as input_DMC
from tblTcInfo i join
[dbo].[tblC] c
on i.PSN = c.PSN join
Daten d
on i.DMC = d.LGNR join -- I am guessing where these comes from
(select distinct ?.DMC, ci.PSN
from tblTcInfo ci join
dbo.tblC c2
on ci.PSN = c2.PSN
where ?.DMC in ('75044500718481203100--1802290018')
) cc
on a.PSN = cc.PSN;