How to insert 100 records in a SQL Server table at the same time [closed]

-5

Could you support me? I need to insert 100 records at the same time in a user table and I know that it can be done through a query, unfortunately I do not know much about SQL to do so and insert one at a time would be very slow.

The table contains these fields

UsuarioId   Numcontrol   Nombre   Apellido
    
asked by Programador S 05.07.2018 в 16:43
source

3 answers

1

You could do it in multiple ways:

Direct

INSERT INTO mi_tabla ( columna1, columna2 ) VALUES
( Valor1, Valor2 ), ( Valor1, Valor2 )

example:

INSERT INTO mi_tabla ( columna1, columna2, columna3 )
VALUES ('John', 123, 'Mexico'), 
('Juan', 124, 'Italia'), 
('Bily', 125, 'Londres'),
('Miranda', 126, 'Belgica');

using a while

declare @id int 
select @id = 1
while @id >=1 and @id <= 100
begin
    insert into estudiantes values(@id, 'estudiante_' + convert(varchar(5), @id), 12)
    select @id = @id + 1
end

In this method what it does is insert data directly, both the id, and the name ..

    
answered by 05.07.2018 / 16:56
source
-1

Concatenandolos will be worth you

INSERT INTO Tabla(id, nombre) VALUES 
(1, 'Pepe'), 
(2, 'Paco'), 
(3, 'Juan');
    
answered by 05.07.2018 в 16:49
-1

In addition to the answers you have been given, if you have the data in another table, you can do a

INSERT INTO SELECT

INSERT INTO Tabla1 (UsuarioId, Numcontrol, Nombre, Apellido)
SELECT (columna1, columna2, columna3, columna4)
FROM Tabla2
WHERE condiciones
    
answered by 05.07.2018 в 17:02