Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
StudentTable
CREATE TABLE [Student].[StudentDetails](
	[StudentId] [int] IDENTITY(1,1) NOT NULL,
	[Name] [varchar](35) NULL,
	[IDNo] [varchar](10) NULL,
	[NameofGuardian] [varchar](50) NULL,
	[AddressofCommunication] [varchar](200) NULL,
	[MobileNumber] [varchar](50) NULL,
	[LandlineNumber] [varchar](20) NULL,
	[Email] [varchar](50) NULL,
	[AdmissionYear] [varchar](20) NULL,
	[SectionID] [int] NULL,
	[ClassID] [int] NULL,
	[IsActive] [bit] NULL,
	[IsDeleted] [bit] NULL,
	[CreatedBy] [int] NULL,
	[CreationDate] [datetime] NULL,
	[ModifiedBy] [int] NULL,
	[ModifiedDate] [datetime] NULL
) 


USER DEFINED TABLE TYPE
CREATE TYPE [Student].[InsertBulkType] AS TABLE(
	[Name] [varchar](35) NULL,
	[IDNo] [varchar](10) NULL,
	[NameofGuardian] [varchar](50) NULL,
	[AddressofCommunication] [varchar](200) NULL,
	[MobileNumber] [varchar](50) NULL,
	[LandlineNumber] [varchar](20) NULL,
	[Email] [varchar](50) NULL,
	[AdmissionYear] [varchar](20) NULL,
	[SectionID] [int] NULL,
	[ClassID] [int] NULL
)


when i create procedure for bulk insert below error came
this is my procedure
Create PROCEDURE [Student].[InsertBulkStudent]
      @tblStudent InsertBulkType READONLY
	 
	
AS
BEGIN

 INSERT INTO StudentDetails
      SELECT Name,IDNo,NameofGuardian,AddressofCommunication,MobileNumber,LandlineNumber,
	  Email,AdmissionYear,SectionID,ClassID
      FROM @tblStudent
  
end


error is
Msg 213, Level 16, State 1, Procedure InsertBulkStudent, Line 9
Column name or number of supplied values does not match table definition.


What I have tried:

Msg 213, Level 16, State 1, Procedure InsertBulkStudent, Line 9
Column name or number of supplied values does not match table definition.
Posted
Updated 15-Dec-18 23:31pm

As your actuall number of columns in the database table and user defined table are different in numbers, you will need to explicitly specify the columns of the db table in the INSERT Statement like:
SQL
INSERT INTO StudentDetails(Name,IDNo,NameofGuardian,AddressofCommunication,MobileNumber,
              LandlineNumber,Email,AdmissionYear,SectionID,ClassID)
     SELECT Name,IDNo,NameofGuardian,AddressofCommunication,MobileNumber,
            LandlineNumber,Email,AdmissionYear,SectionID,ClassID
     FROM @tblStuden
 
Share this answer
 
v2
Your table contains a first column which you don't - and can't - specify: the StudentID column. You can't specify it because it's an IDENTITY column, but if you do not list the columns to insert to, then SQL always starts at the first and proceeds to there. There is no command to "skip a column" so you need to specify the column names you want data to go into. You should really do that anyway, it's good practice!

SQL
Create PROCEDURE [InsertBulkStudent]
      @tblStudent InsertBulkType READONLY
	 
	
AS
BEGIN

 INSERT INTO StudentDetails (Name,IDNo,NameofGuardian,AddressofCommunication,MobileNumber,LandlineNumber,
	  Email,AdmissionYear,SectionID,ClassID) 
      SELECT Name,IDNo,NameofGuardian,AddressofCommunication,MobileNumber,LandlineNumber,
	  Email,AdmissionYear,SectionID,ClassID
      FROM @tblStudent
  
end
 
Share this answer
 
Comments
Member 14024377 16-Dec-18 10:49am    
@OriginalGriff
Thank You Very Much
OriginalGriff 16-Dec-18 11:07am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900