I am trying to retrieve a survey-dataset from two different tables in mssql. Every survey has a few options for answering, which should be returned in an array as a part of the retrieved data table.
Basically, the returned data table should be like that:
SurveyName xyz (from table no.1)
SurveyTopic abc (from table no.1)
Optionen [d, v, q, o] (from table no.2)
What is the best way of achieving this? Creating a defined data type? I tried using a join-argument but then the data gets mixed up and returned as if there were different surveys with one option each. The code looks like this so far:
CREATE PROCEDURE dbo.GetSurvey
@Id nvarchar (128)
AS
SELECT Surveys.*, Options,*
FROM dbo.Surveys
JOIN Options
ON Options.SurveyId = Survey.Id
WHERE Surveys.Id = @Id
Here is some sample data as json-files:
Surveys
{
"id": "3f07153f-78cf-4b03-a442-5dbbbdc6c85d",
"topic": "Internet connection",
"question": "How fast is you connection?"
}
Option (each survey has a few of those)
{
"id":"3b3f9583-7d09-49d2-baee-d724c6ce3d9d",
"surveyId":"3f07153f-78cf-4b03-a442-5dbbbdc6c85d",
"answer":"10mbit/s"
}
What the table should look like:
{
"id": "3f07153f-78cf-4b03-a442-5dbbbdc6c85d",
"topic": "Internet connection",
"question": "How fast is you connection?"
"options":[
{"id":"85c1ae87-7da9-41c4-9046-22df231af6ec",
"surveyId":"3f07153f-78cf-4b03-a442-5dbbbdc6c85d",
"answer":"10mbit/s"}
{"id":"55347f11-c01f-4b5f-86a3-9d9c66c2aef5",
"surveyId":"3f07153f-78cf-4b03-a442-5dbbbdc6c85d",
"answer":"20mbit/s"}
]
}
