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

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

			SELECT COUNT(*) AS RecordsIn FROM tmp.OrdersHistRaw
		END
END