从子查询中选择多列 [英] Selecting multiple columns from a subquery

查看:47
本文介绍了从子查询中选择多列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了很多,但仍然没有机会让子查询一次返回多个列.以下代码有效,但很糟糕:

I've searched a lot, but still no chance on having a subquery to return multiple columns all at once. The following code works, but it sucks:

SELECT
    (SELECT Column1 FROM dbo.fnGetItemPath(ib.Id)) AS Col1,
    (SELECT Column2 FROM dbo.fnGetItemPath(ib.Id)) AS Col2,
    (SELECT Column3 FROM dbo.fnGetItemPath(ib.Id)) AS Col3
FROM ItemBase ib

我实际上不知道如何将 ib.Id 传递给函数并在不调用 fnGetItemPath 函数 3 次的情况下获取整个 Column1、Column2、Column3 列.

I actually have got no idea how to pass ib.Id to the function and get the entire Column1, Column2, Column3 columns without calling the fnGetItemPath function 3 times.

提前致谢

推荐答案

您可以将 ti 移至FROM"部分并使用 外部应用(或交叉应用).

You can move ti to "FROM" part and use outer apply (or cross apply).

自己检查语法,但它应该看起来像这样:

check syntax yourself, but it should look something like this:

SELECT Column1, Column2, Column3
FROM ItemBase ib
Outer Apply dbo.fnGetItemPath(ib.Id)

这篇关于从子查询中选择多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆