﻿CREATE  PROCEDURE [dbo].[sp_InsertIdSezioniIntoReportSezioniPers]
@pIdReport as int,
@delimiter as char(1),
@splitstring as varchar(2000),
@utente as varchar(13),
@RifPianificazione as bit = null
AS
BEGIN
DECLARE @valore int
DECLARE @idReport as int
DECLARE @incremento int
SET @idReport = 0
SET @incremento = 0
SELECT @idReport=idReport  FROM [dbo].ReportSezionePers WHERE IdReport = @pIdReport
if (@idReport > 0)
		  BEGIN
				DELETE FROM [dbo].[ReportSezionePers]
				WHERE IdReport = @idReport --and Utente = @utente		
		  END
		  DECLARE db_cursor CURSOR FOR 	
		  SELECT 	s.[Value]
		  FROM 		dbo.Split( @delimiter, @splitstring ) AS s
		  OPEN db_cursor   
		  FETCH NEXT FROM db_cursor INTO @valore		  	  
		  WHILE @@FETCH_STATUS = 0   
				BEGIN
					-- insert	
					INSERT INTO DBO.REPORTSEZIONEPERS
					select 	getdate(), 
							null, 
							@pIdReport, 
							@valore, 
							@utente,
							@RifPianificazione,
							@incremento
					FETCH NEXT FROM db_cursor INTO @valore   
					SET @incremento = @incremento + 1;	
				END   
			CLOSE db_cursor   
			DEALLOCATE db_cursor 
			RETURN @incremento
END