Академический Документы
Профессиональный Документы
Культура Документы
1. BULK INSERT
2.
[ database_name . [ schema_name ] . | schema_name . ] [ table_name | view_name ]
3.
FROM 'data_file'
4.
[ WITH
5.
(
6.
[ [ , ] BATCHSIZE = batch_size ]
7.
[ [ , ] CHECK_CONSTRAINTS ]
8.
[ [ , ] CODEPAGE = { 'ACP' | 'OEM' | 'RAW' | 'code_page' } ]
9.
[ [ , ] DATAFILETYPE =
10.
{ 'char' | 'native'| 'widechar' | 'widenative' } ]
11.
[ [ , ] FIELDTERMINATOR = 'field_terminator' ]
12.
[ [ , ] FIRSTROW = first_row ]
13.
[ [ , ] FIRE_TRIGGERS ]
14.
[ [ , ] FORMATFILE = 'format_file_path' ]
15.
[ [ , ] KEEPIDENTITY ]
16.
[ [ , ] KEEPNULLS ]
17.
[ [ , ] KILOBYTES_PER_BATCH = kilobytes_per_batch ]
18.
[ [ , ] LASTROW = last_row ]
19.
[ [ , ] MAXERRORS = max_errors ]
20.
[ [ , ] ORDER ( { column [ ASC | DESC ] } [ ,...n ] ) ]
21.
[ [ , ] ROWS_PER_BATCH = rows_per_batch ]
22.
[ [ , ] ROWTERMINATOR = 'row_terminator' ]
23.
[ [ , ] TABLOCK ]
24.
[ [ , ] ERRORFILE = 'file_name' ]
25.
)]
1.
2.
3.
4.
5.
6.
7.
8.
BULK INSERT
[ database_name . [ schema_name ]
FROM 'data_file'
[ WITH
(
[ BATCHSIZE = batch_size ]
[ [ , ] FORMATFILE = 'format_file_path' ]
)]
Para este proceso debemos tener un archivo de formato, nuestro archivo de carga y obviamente
nuestra tabla de destino. Para este ejemplo utilizaremos los siguiente:
Tabla cliente
view plaincopy to clipboardprint?
1.
2.
3.
4.
5.
6.
7.
1.
2.
3.
4.
5.
10.0
3
1 SQLCHAR 0 30 "" 1 nombre SQL_Latin1_General_CP1_CI_AS
2 SQLCHAR 0 10 "" 2 rut SQL_Latin1_General_CP1_CI_AS
3 SQLCHAR 0 10 "\r\n" 3 telefono SQL_Latin1_General_CP1_CI_AS
Los nombres y largos de los campos deben coincidir con los de la base de datos.
Si lo dejamos as separara el archivo por cantidad de caracteres si lo quieren por delimitadores se
deben agregar en (ejemplo ;).
La ultima fila debe llevar siempre \r\n
10.0 Corresponde a la versin utilizada por sql server
3 Corresponde a la cantidad de filas
Archivo de Carga (cliente_masivo.txt)
1.
2.
3.
4.
5.
6.
Pedro
Pedro
Pedro
Pedro
Pedro
.....
Juan
Juan
Juan
Juan
Juan
Lopez
Lopez
Lopez
Lopez
Lopez
Ortega
Ortega
Ortega
Ortega
Ortega
11111111-80991234567
11111111-80991234567
11111111-80991234567
11111111-80991234567
11111111-80991234567
Bueno con estos datos estamos listos para realizar nuestra carga de datos masivos. Para no alargar
mas este tutorial les dejo el procedimiento almacenado y la forma de uso:
view plaincopy to clipboardprint?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
Al comienzo limpia la tabla (en caso que no necesiten limpiar solo borran esa parte).
Controla algunos errores que puedan ocurrir retornando -1.
Modo de uso:
view plaincopy to clipboardprint?
1.
dbo.SpCargaMasiva 'C:\cliente.txt','C:\cliente.fmt','cliente'
Si estn trabajando apuntando a algn servidor por ejemplo QUALITY deben cambiar la ruta C:\ por la
correspondiente \\QUALITY\