title: Storing json array in SQL table
body: <p>I have two tables: user and phone. The table phone has a reference to the table user. This allows several phone numbers for each user.</p>
  
  <p>I am sending the information to the SQL server in json format, but I am struggeling to store the phone numbers. This is what I have so far:</p>
  
  <pre><code>CREATE TABLE [dbo].[user](
      [id] [int] IDENTITY(1,1) NOT NULL,
      [name] [varchar](max) NULL
  ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
  GO
  
  CREATE TABLE [dbo].[phone](
      [id] [int] IDENTITY(1,1) NOT NULL,
      [userId] [int] NOT NULL,
      [type] [varchar](12) NOT NULL,
      [phone] [varchar](12) NOT NULL
  ) ON [PRIMARY]
  GO
  
  DECLARE @jsonVariable NVARCHAR(MAX)
  
  SET @jsonVariable = N'{
     "name":"Joe Smith",
     "phones":[
        {
           "phone":"+1 123 123-4567",
           "type":"business"
        },
        {
           "phone":"+1 987 987-6543",
           "type":"mobile"
        }
     ]
  }'
  
  DECLARE @userId int
  DECLARE @userName varchar(max) = (SELECT userName FROM OPENJSON (@jsonVariable) WITH (userName varchar(max) N'$.name'))
  INSERT INTO [dbo].[user] ([name]) VALUES (@userName)
  
  SET @userId = @@IDENTITY
  </code></pre>
  
  <p>The question now is: is there something like a "forEach" routine to store all phone numbers?</p>