﻿

-- =============================================
-- Author:		S.E. Locatelli
-- Create date: 2013.12.02
-- Description:	Procedura per conversione e tipizzazione dati da tmp a ext x DiesRaw
-- =============================================
CREATE PROCEDURE [tmp].[convertDiesRaw]
(
	 @fileType NVARCHAR(50) = 'DiesRaw'
)
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 = 'DiesRaw'
		BEGIN
			-- svuoto tabella temp
			TRUNCATE TABLE ext.DiesRaw

			-- inserisco con cast valori...
			INSERT ext.DiesRaw
            WITH (TABLOCK)
            ( CodDies, HoleNum, CodItem, CodDiesGroup, Price, ExpLife, ActLife, Active ) 
            SELECT
                   CodDies,
                   CAST(ISNULL(HoleNum,0) AS INT) AS HoleNum,
				   CodItem,
				   CodDiesGroup,
                   CAST( CAST( REPLACE( '0' + Price,',','.' )AS float )AS decimal( 9,3 ))  AS Price,
                   CAST( CAST( REPLACE( '0' + ExpLife,',','.' )AS float )AS decimal( 9,2 ))  AS ExpLife,
                   CAST( CAST( REPLACE( '0' + ActLife,',','.' )AS float )AS decimal( 9,2 ))  AS ActLife,
                   CAST(ISNULL(Active,0) AS INT) AS Active
            FROM tmp.DiesRaw;

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

END


