【发布时间】:2022-01-14 00:43:13
【问题描述】:
select * from openquery(servername,'select vndr_num as VendorNum,t1,t2,t3,
case when trim(t1) is not null then t1
when trim(t2) is not null then t2
else t3
end as TAX,
coalesce(t1,t2,t3) as TID
from Mck.table_name')
我希望当 t1 为空白时,应将 t2/t3 列中的数据复制到 TAx 和 TID 列,但在 和 coalesce 函数的情况下,结果是相同的。如果 case when 和 coalesce 无法做到这一点,请提出任何其他选项。
Vnum t1 t2 t3 Tax TID
1
2
3 86-0378223
4 95-2743174
5 81-3997819
6 532-98-0158 532-98-0158 532-98-0158
7 82-2010249
8 46-2037641
9 32-0119411
【问题讨论】:
-
如果你直接在目标服务器上执行查询,而不是通过openquery,你会得到所需的结果吗?
-
我只想要 openquery 所需的结果,因为我正在从该服务器连接到多个服务器。我怀疑 t1 列中有一些隐藏值,因为合并函数没有向前读取 t2/t3 值。如果是这种情况,我们可以用修剪删除那些隐藏的值吗?
标签: sql sql-server tsql