0

I am trying to select JSON properties as column. The properties that needs to be selected are stored in some other table

CREATE TABLE [Templates](
    [ID] [int] NOT NULL,
    [Template] [nvarchar](max)
)

INSERT INTO Templates(ID,Template)
VALUES
(1,'{"FirstName":"foo"}'),
(2,'{"FirstName":"joe","LastName":"dow"}'),
(3,'{"LastName":"smith","Address":"1234 Test Drive"}'),
(4,NULL)


Declare @SQL NVARCHAR(max)

--For brevity purpose i am hardcoding these property names. In reality column names will come from another table
DECLARE @properties NVARCHAR(max) = '[Address],[FirstName],[LastName]'

Set @SQL  = '
Select '+ @properties +'
 From  (
        Select T.ID
              ,j.[Key]
              ,j.[Value]
         From  Templates T
         Cross Apply OpenJSON(T.Template) AS j
       ) src
Pivot ( max(value) for [Key] in ('+ @properties +') ) pvt 
'
Exec(@SQL)

SQL Fiddle

The code above is mostly working except for the Last row where Template is NULL. (ID = 4)
Expected: The query should return 4 rows and for the last row, all columns should be null except ID.

1
  • Change CROSS APPLY to OUTER APPLY. Commented Aug 5, 2020 at 21:50

1 Answer 1

1

CROSS APPLY requires rows to be returned, however, Cross Apply OpenJSON(NULL) AS j returns no rows. Use OUTER APPLY:

' ...
OUTER APPLY OpenJSON(T.Template) AS j
...'

EXEC sp_executesql @SQL;

Note I also use sp_executesql as you can't parametrise EXEC (@SQL)

db<>fiddle

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.