I have a stored procedure that I want to use to create one row in a parent table [Test] and multiple rows in a child table [TestQuestion]. The parent and child table both have primary keys that are identity datatype. Here is what the child table looks like with some not relevant columns removed:
CREATE TABLE [dbo].[TestQuestion] (
[TestQuestionId] INT IDENTITY (1, 1) NOT NULL,
[TestId] INT NOT NULL,
[QuestionNumber] INT NOT NULL
);
Inserting into the parent table is easy as all parameters are supplied to the SP and I just map these to an insert and perform the insert. But the child data table ids are given as a parameter @qidsJSON
containing ids in a JSON form like this:
parameterList.Add(new SqlParameter ("@qidsJSON", qids.ToJSONString()));
["3CEFF956-BF61-419E-8FB2-9D6A1B75B909","63E75A2D-9F45-43CC-B706-D9890A22577D"]
Is there a way that I can use TransactSQL to take the data from my @qidsJSON
and
have it insert a row into the TestQuestion table for every GUID that appears in the parameter?
Alternatively is there another way I could pass data in a parameter that contains mulitple GUIDs? I am using C# to formulate the input data from a C# List so I could create the data for the input parameter in any form that would be most easy for the stored procedure to use.