In my previous blog, I explained how we can achieve bulk insertion with Table value types in Entity Framework. After that, while working with Node.js API, I encountered with the same scenario there also, where I neede to insert bulk data from the application.
At that time, this clicked in my mind that many of us may have faced the same issue and if it is possible to use Table value type in Node.js.
Prerequisites
- MS SQL Server
- Node.js installed with packages (Express, MSSql, and body-parser)
Step 1
Create Table value type.
- CREATE TYPE [dbo].[Tvp_Employee] AS TABLE(
- [Name] [varchar](50) NULL,
- [Salary] [numeric](18, 0) NULL
- )
- GO
Step 2
After creating Table value type, create a stored procedure that will use the created Table value type as parameter.
- Create PROCEDURE [dbo].[Proc_insertemployee] (@tbl_Employee TVP_EMPLOYEE readonly)
- AS
- BEGIN
- BEGIN try
- -- Insert statements for procedure here
- INSERT INTO tbl_employee
- (NAME,
- salary)
- SELECT NAME,
- salary
- FROM @tbl_Employee
- Select 1 as 'Code', 'Inserted Successfuly.' as 'Message'
- END try
- BEGIN catch
- DECLARE @ErrorNumber INT
- DECLARE @ErrorMessage VARCHAR(2000)
- DECLARE @ErrorSeverity INT
- DECLARE @ErrorState INT
- SELECT @ErrorNumber = Error_number(),
- @ErrorMessage = 'Error occured at time of inserting'
- + Error_message(),
- @Errorseverity = Error_severity(),
- @ErrorState = Error_state()
- RAISERROR (@Errormessage,@ErrorSeverity,@ErrorState)
- END catch
- END

Pavel MelnikPosted Feb 12, 2020, 11:07 PM
Hello Mitesh! Thanks for the article! Could you please provide a full code example?