title: Inserting into many-to-many table in SQL Server
body: <p>This is my <code>Tag</code> table:</p>
  
  <pre><code>CREATE TABLE [dbo].[Tag](
      [Id] [int] IDENTITY(1,1) NOT NULL,
      [Name] [nvarchar](max) NULL,
      [CreationDate] [datetime] NOT NULL,
      [TagSlug] [nvarchar](max) NOT NULL,
  
      PRIMARY KEY CLUSTERED ([Id] ASC)
  ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
  </code></pre>
  
  <p>and this is my <code>Post</code> table:</p>
  
  <pre><code>CREATE TABLE [dbo].[Post](
      [Id] [int] IDENTITY(1,1) NOT NULL,
      [Title] [nvarchar](400) NOT NULL,
      [Body] [nvarchar](max) NOT NULL,
      [Summary] [nvarchar](max) NOT NULL,
      [CreationDate] [datetime] NOT NULL,
      [UrlSlug] [nvarchar](max) NOT NULL,
      [Picture] [nvarchar](max) NULL,
      [TagId] [int] NOT NULL,
  
      PRIMARY KEY CLUSTERED ([Id] ASC)
  ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
  GO
  
  ALTER TABLE [dbo].[Post]  WITH CHECK ADD  CONSTRAINT [Post_Tag] FOREIGN KEY([TagId])
  REFERENCES [dbo].[Tag] ([Id])
  ON DELETE CASCADE
  GO
  
  ALTER TABLE [dbo].[Post] CHECK CONSTRAINT [Post_Tag]
  GO
  </code></pre>
  
  <p>I just wanna to insert the <code>Id</code> from <code>Tag</code> and <code>PostId</code> from <code>Post</code> into a new table named <code>Post_Tag</code> which is a many to many relation, this is the script of my <code>Post_Tag</code> table:</p>
  
  <pre><code>CREATE TABLE [dbo].[Post_Tag](
      [PostId] [int] NOT NULL,
      [TagId] [int] NOT NULL,
  
      CONSTRAINT [PK_dbo.Post_Tag] PRIMARY KEY CLUSTERED ([PostId] ASC, [TagId] ASC)
  ) ON [PRIMARY]
  GO
  
  ALTER TABLE [dbo].[Post_Tag]  WITH CHECK 
  ADD CONSTRAINT [FK_dbo.Post_Tag_dbo.Post_PostId] 
  FOREIGN KEY([PostId]) REFERENCES [dbo].[Post] ([Id])
     ON DELETE CASCADE
  GO
  
  ALTER TABLE [dbo].[Post_Tag] CHECK CONSTRAINT [FK_dbo.Post_Tag_dbo.Post_PostId]
  GO
  
  ALTER TABLE [dbo].[Post_Tag] WITH CHECK 
  ADD CONSTRAINT [FK_dbo.Post_Tag_dbo.Tag_TagId] 
  FOREIGN KEY([TagId]) REFERENCES [dbo].[Tag] ([Id])
     ON DELETE CASCADE
  GO
  
  ALTER TABLE [dbo].[Post_Tag] CHECK CONSTRAINT [FK_dbo.Post_Tag_dbo.Tag_TagId]
  GO
  </code></pre>
  
  <p>Now, to do that I've tried the below query:</p>
  
  <pre><code>insert into [Blog].[dbo].[Post_Tag] (PostId,TagId)
     select [Id] as [PostId] from [OldBlog].[dbo].[Tag]
     select [TagId] from [OldBlog].[dbo].[Post]
  </code></pre>
  
  <p>but this error appear while running the script:</p>
  
  <blockquote>
    <p>The select list for the INSERT statement contains fewer items than the insert list. The number of SELECT values must match the number of INSERT columns.</p>
  </blockquote>
  
  <p>what's wrong with my query? thanks</p>