SQL Server No column name was specified for column 1 of 'sp'

mfpqipee  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(177)

I have following query:

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

I'm trying to build json array according to OUTER APPLY result but I stuck here because there is an error No column name was specified for column 1 of 'sp'. I found similar question here on stackoverflow, but there is no example with outer apply.

Can you explain me what is wrong with this query?

qyswt5oh

qyswt5oh1#

You just need to add a column alias to your applied result sp , e.g. sp(json) :

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) -- <-- Added alias

You can then access this in your overall results as either sp.* or sp.json , since the column is now named.

相关问题