﻿CREATE  PROCEDURE [dbo].[sp_getLastSavedUserModelNew]
@pIdReportModello as int,
@pUtente as varchar(13) = null,
@pTpReport as int,
@idCliente as int
AS
BEGIN
    if @pUtente is not null
		SELECT     TOP (1) Report.IdReport, Report.Nome, Report.Descrizione
		FROM         Report INNER JOIN
							  ReportPerCliente ON Report.IdReport = ReportPerCliente.IdReport
		WHERE     (Report.Utente = @pUtente) AND (Report.Nome NOT LIKE '%LAST USED CONFIG%') AND (Report.IdReportModello = @pIdReportModello) AND (Report.TpReport = @pTpReport) and ReportPerCliente.IdCliente = @idCliente
		ORDER BY Report.DtCreazione DESC
    else
		SELECT     TOP (1) Report.IdReport, Report.Nome, Report.Descrizione
		FROM         Report INNER JOIN
							  ReportPerCliente ON Report.IdReport = ReportPerCliente.IdReport
		WHERE     (Report.Nome NOT LIKE '%LAST USED CONFIG%') AND (Report.IdReportModello = @pIdReportModello) AND (Report.TpReport = @pTpReport) and ReportPerCliente.IdCliente = @idCliente
		ORDER BY Report.DtCreazione DESC
END