﻿


-- =============================================
-- Author:		S.E. Locatelli
-- Mod. date: 2013.11.29
-- Description:	Procedura per import dati in tabella tmp da file csv
-- =============================================
CREATE PROCEDURE [tmp].[importClientsRaw]
(
	 @fileType NVARCHAR(50) = 'ClientsRaw'
)
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 = 'ClientsRaw'
		BEGIN
			-- svuoto tabella temp
			TRUNCATE TABLE tmp.ClientsRaw

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

           	SELECT COUNT(*)  as RecordsIn FROM tmp.ClientsRaw

		END

END


