﻿

-- =============================================
-- Author:		S.E. Locatelli
-- Create date: 2014.01.16
-- Description:	Procedura per import dati in tabella tmp da file csv
-- =============================================
create PROCEDURE [tmp].[importTranspCostRaw]
(
	 @fileType NVARCHAR(50) = 'TranspCostRaw'
)
AS
BEGIN
	SET NOCOUNT ON;

	-- per Giancarlo: rendere parametrico con esecuzione sql x poter inserire nei parametri il separatore ed il resto? http://www.sqlteam.com/article/using-bulk-insert-to-load-a-text-file
	
	IF @fileType = 'TranspCostRaw'
		BEGIN
			-- svuoto tabella temp
			TRUNCATE TABLE tmp.TranspCostRaw

			-- carico dati bulk!
			BULK INSERT tmp.TranspCostRaw
			FROM 'e:\test\TranspCostRaw.csv'
			WITH (FIELDTERMINATOR = ';', ROWTERMINATOR = '\n', FIRSTROW = 2, ERRORFILE = 'e:\test\errorTranspCost.log')
			--WITH (FIELDTERMINATOR = ';', ROWTERMINATOR = '\n', FIRSTROW = 2, ERRORFILE = 'e:\test\errorTranspCost.log', CODEPAGE = '1252')

           	SELECT COUNT(*)  as RecordsIn FROM tmp.TranspCostRaw

		END

END


