David David - 5 months ago 9
SQL Question

Insert several values in SQL Server

I'm trying to insert some values into SQL Server but I get an error message:


Line 2: Incorrect syntax near ','


I need to enter several values in 1 table so I was looking for a quicker way to not insert 1 at a time.

For some reason I always have issues with databases :(

My query is:

INSERT INTO PERSONALRH_NIVEL (SERHGCCAB, PUESTO_ID, COMPANIA_ID, REGION_ID, TIPO_EMPLEADO)
VALUES (81570, 4, 2001, 2, 'N'),
(81570, 4, 2001, 3, 'S'),
(81570, 4, 2001, 3, 'N');


Thank you in advance, David

Answer

For older versions of SQL Server you can use insert from select

INSERT INTO PERSONALRH_NIVEL (SERHGCCAB, PUESTO_ID, COMPANIA_ID, REGION_ID, TIPO_EMPLEADO)
SELECT 81570, 4, 2001, 2, 'N' 
UNION ALL
SELECT 81570, 4, 2001, 3, 'S'
UNION ALL
SELECT 81570, 4, 2001, 3, 'N';