I am working on SQL Server 2008. I have to generate json data from the customer table and insert into the CustomerJSON
table. SQL Server 2016 has native JSON capabilities, but unfortunately, I have to work with SQL Server 2008 only.
I have used the following SQL statements:
Insert into CustomerJSON (Email, CustomerData)
select
Email,
stuff((select ',' + '{"email":' + email +
',"firstName":"' + FirstName + '"' +
',"lastName":"' + LastName + '"' +
',"phoneNumber":"' + PhoneNumber+ '"' +'}'
from Customer
for xml path('')), 1, 1, '')
from
Customer
As you can see in the screenshot, the JSON generated has all the four rows' data. However, I want JSON to be constructed from each row of the Customer table and not concatenate all. For reference, I used, SQL Server SELECT to JSON function