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?
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.