﻿

-- =============================================
-- Author:		S.E. Locatelli
-- Create date: 2013.11.28
-- Description:	Procedura per conversioen e tipizzazione dati da tmp a ext x TranspCostRaw
-- =============================================
CREATE PROCEDURE [tmp].[convertTranspCostRaw]
(
	 @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 ext.TranspCostRaw

			-- inserisco con cast valori...
			INSERT	ext.TranspCostRaw
            WITH (TABLOCK)
					(TranspZone, TranspCost) 
            SELECT
				   TranspZone,
                   MAX(CAST( CAST( REPLACE( '0' + TranspCost,',','.' )AS float )AS decimal( 18,9 )))  AS TranspCost
            FROM tmp.TranspCostRaw
			GROUP BY TranspZone

			SELECT COUNT(*)  as RecordsIn FROM ext.TranspCostRaw
		END

END


