fjtello / SQL-C-sharp

Common programming and coding tips and howtos
0 stars 0 forks source link

SQL syntax [#bulk insert #exec] #16

Open fjtello opened 7 years ago

fjtello commented 7 years ago

IF(OBJECT_ID('tempdb..#tmpInsercion') IS NOT NULL) BEGIN DROP TABLE tempdb..#tmpInsercion; END

CREATE TABLE #tmpInsercion ( id INT UNIQUE, name VARCHAR(20), description VARCHAR(250), checked int );

DECLARE @archivo AS VARCHAR(200); DECLARE @varbulk AS VARCHAR(2000);

-- Archivo visible desde el servidor de SQL Server SET @archivo = '\servidor\ruta1\datafile.csv';

SET @varbulk = 'BULK INSERT #tmpInsercion FROM ''' + @archivo + ''' WITH (FIELDTERMINATOR = '';'', KEEPNULLS, ROWTERMINATOR = ''' + CHAR(10) + ''')'; -- Ojo, el rowterminator es una opción, en texto plano, sería sin esa parte / BULK INSERT #tmpInsercion FROM '\servidor\ruta1\datafile.csv' WITH (FIELDTERMINATOR = ';', KEEPNULLS, ROWTERMINATOR = ' '); /

EXEC(@varbulk);

SELECT * FROM tempdb..#tmpInsercion