-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
-- Inserimento nuovo [C6Mart].[GESTIONE_INSERT_ZIP] -1,'NomeFileOk','NomeFileOk'
CREATE procedure [C6Mart].[GESTIONE_INSERT_ZIP]
@NomeFile as varchar(255),
@NomeCartella as varchar(255)
AS
BEGIN 
BEGIN TRANSACTION
DECLARE @ID_ELAB INT
SET @ID_ELAB = C6Mart.GETIDELAB()
declare @IdZip as int
set @IdZip = -1
--recupero idzip se Esiste insieme al NomeCartella
SELECT @IdZip=id,@NomeCartella=NomeCartella 
FROM
	C6Mart.GESTIONE_ZIP_FTP
WHERE
	FlagInvio = 'N'
IF(@IDZIP = -1)--NON ESISTE
	BEGIN
	 INSERT INTO [C6MART].[GESTIONE_ZIP_FTP]
           ([NOMEFILE]
           ,[DATAINVIO]
           ,[FLAGINVIO]
           ,[NOMECARTELLA]
           ,[ID_ELAB])
     VALUES
           (@NOMEFILE
           ,NULL
           ,'N'
           ,@NOMECARTELLA
           ,@ID_ELAB)
	SET @IDZIP = SCOPE_IDENTITY()
	END
ELSE --ESISTE
	BEGIN
		 UPDATE [C6MART].[GESTIONE_ZIP_FTP]
		 SET [NOMEFILE] = @NOMEFILE
		 WHERE ID = @IDZIP
	END
IF @@error <> 0
	begin
		rollback
	end
else
	begin
		commit
		select @IdZip IdZip,@NomeFile NomeFile,@NomeCartella NomeCartella
	end
end