﻿CREATE  PROCEDURE [dbo].[sp_getAllTemplateByIdCliente]
@codicePB as  varchar(6),
@rete as char,
@idmodello as int
AS
BEGIN
--SELECT [idTemplate]
--       ,[codicePB]
--       ,[nomeTemplate]
--	   ,[default]
--	   ,rete
--	   ,DtCreazione
--	   ,opznominativa
--       ,opzterzi
--  FROM [SEIReport].[dbo].[R6_ReportSchede_Template]
--WHERE (codicePB = @codicePB or (codicePB='' and [default]='S')) and rete= @rete and idmodello = @idmodello
--order by (case when [default]='s' then '*-'+nomeTemplate else nomeTemplate end)
select * FROM
(
SELECT [idTemplate]
       ,[codicePB]
       ,[nomeTemplate]
	   ,[default]
	   ,rete
	   ,DtCreazione
	   ,opznominativa
       ,opzterzi      
FROM [SEIReport].[dbo].[R6_ReportSchede_TemplateDefault]
WHERE  rete= @rete and idmodello = @idmodello
 union 
SELECT [idTemplate]
       ,[codicePB]
       ,[nomeTemplate]
	   ,[default]
	   ,rete
	   ,DtCreazione
	   ,opznominativa
       ,opzterzi
  FROM [SEIReport].[dbo].[R6_ReportSchede_Template] 
  WHERE codicePB = @codicePB and rete= @rete and idmodello = @idmodello and [default] <> 'S'
 ) T 
order by (case when [default]='s' then '*-'+nomeTemplate else nomeTemplate end)
END