没有为“ sp”的列1指定列名

时间:2019-07-16 07:02:01

标签: sql-server

我有以下查询:

SELECT s."Description",
sp.*
FROM "Supplier" as s
OUTER APPLY (
    SELECT p."Id", p."Description", p."Price"
    FROM "Products" as p
    WHERE p."SupplierId" = s."Id"
    FOR JSON auto 
) as sp

我正在尝试根据OUTER APPLY的结果构建json数组,但由于存在错误No column name was specified for column 1 of 'sp'.,我在这里卡住了,我在stackoverflow上发现了类似的问题,但是没有适用于外部的示例。

您能解释一下该查询出了什么问题吗?

1 个答案:

答案 0 :(得分:3)

您只需要添加列别名:

SELECT 
    s."Description",
    sp.*
FROM "Supplier" as s
OUTER APPLY (
    SELECT p."Id", p."Description", p."Price"
    FROM "Products" as p
    WHERE p."SupplierId" = s."Id"
    FOR JSON auto 
) as sp(json)