One of my colleague was facing an issue while inserting 2000 values to a table variable as below:
DECLARE @EMPLOYEEDETAILS TABLE
(OLDEMPLOYEECODE NVARCHAR(30), NEWEMPLOYEECODE NVARCHAR(30))
INSERT INTO @EMPLOYEEDETAILS(OLDEMPLOYEECODE, NEWEMPLOYEECODE)
VALUES
('93466','0000007'),
('93467','0000010'),
...
...(2000 Records)
...
('93467','00002000')
Error Message:
Msg 10738, Level 15, State 1, Line 1006
The number of row value expressions in the INSERT statement exceeds the maximum allowed number of 1000 row values.
This is a self explanatory error message as SQL Server does not support row value expressions for more than 1000 values.
To overcome the issue, the code has been modified as below and the query has been executed successfully.
Solution:
DECLARE @EMPLOYEEDETAILS TABLE
(OLDEMPLOYEECODE NVARCHAR(30), NEWEMPLOYEECODE NVARCHAR(30))
INSERT INTO @EMPLOYEEDETAILS(OLDEMPLOYEECODE, NEWEMPLOYEECODE)
SELECT * FROM (VALUES
('93466','0000007'),
('93467','0000010'),
...
...(2000 Records)
...
('93467','00002000')
) A(Col1, Col2)
You may share any other alternatives/thoughts….
I like it. Any downfalls that you can think of using this compared to BULK INSERT using a file or XML?
LikeLike
Thanks and glad to see that you liked this method.
Yes, you can alternate with BULK INSERT or XML. But XML would be my last choice as it may need some more resource ( more memory) generally than other way of implementing.
LikeLike
Nice solution,worked for me!
LikeLike
Love this, very helpful!
LikeLike
Fantastic! Ran into this today and this solution was just what I was looking for.
LikeLike