zoptymalizuj kod: 


declare
 @aufnr		int			= 824814 --825589 --878052 --824814
,@posnr		int			= 1
,@StrMoc	char(1)		= '-'
,@nrsek		int			= 1
;

DECLARE @Kl25000 TABLE (artklcode int, escode int, wartosc nvarchar(255));
INSERT INTO @Kl25000
SELECT * FROM dbo.fn_brseg_ImaKl_25000(@aufnr, @posnr, 25000, '')

-----------------------------------------------------------------------------------------------------------------------------------------------------------

--- RysY		wartosc pobrana z klasy 1206 ES131
--- RysKorY		wartosc pobrana z klasy 1206 ES3214
--- ImaObrY		wartosc pobrana z klasy 1206 ES131 skorygowana o uszczelkę oraz koryto dolne jeżeli @PROG_DS>0

DECLARE @Kl25304_1206 TABLE (AUFNR int,POSNR int,ARTKLCODE int,ZamUchUmi nvarchar(20),Wysokosc nvarchar(20)
,RodSek nvarchar(20), NrSek nvarchar(20), StrMoc nvarchar(20), Ojciec nvarchar(20), ARTNR nvarchar(20), RysY nvarchar(20), RysKorY nvarchar(20), ImaObrY nvarchar(20));

declare @RysPanSta			int = (select wartosc from @Kl25000 where escode=3211);
declare @PROG_DS			nvarchar(20) = (select wartosc from @Kl25000 where escode=1403);

INSERT INTO @Kl25304_1206
--- parsowanie RysY, RysKor na ImaObr
SELECT * FROM 
	(SELECT 
		*, iif(@nrsek > 1, 
			RysKorY, 
			case when ARTNR='OME_U' 
				 then RysKorY
				 else RysY 
					- case 
						when @PROG_DS=0 
						then cast(@RysPanSta as int)-10 
						else @RysPanSta 
					  end
			end
		
		) as ImaObrY
	FROM dbo.fn_brseg_ImaKl_25304_1206(@aufnr, @posnr)
	) T 
	WHERE NrSek=@nrsek and StrMoc=@StrMoc
;

-----------------------------------------------------------------------------------------------------------------------------------------------------------
declare @K2_I				nvarchar(20) = case (select wartosc from @Kl25000 where escode=1000) when 'K2 I' then '1' else '0' end; 
declare @DS					nvarchar(20) = case (select wartosc from @Kl25000 where escode=1401) when 'J' then '1' else '0' end; 
declare @Ostatnia_DS		nvarchar(20) = case @nrsek when cast((select wartosc from @Kl25000 where escode=3091) as int)  then '1' else '0' end;
declare @Rodzaj_sekcji		nvarchar(20) = case @nrsek when '1' then 'D' when (select wartosc from @Kl25000 where escode=3010) then 'G' else 'S' end;
declare @Okno_umiejsc		nvarchar(20) = (select wartosc from @Kl25000 where escode=1305); 
declare @OknoWykonaj		int			 = case (select RESULT from dbo.CSVtoStringTable(@Okno_umiejsc, '-') where RESULT=@nrsek) when @nrsek then 1 else 0 end;
declare @PioroSek			nvarchar(20) = case (select wartosc from @Kl25000 where escode=1004) when 40 then 25 else 60 end;
---declare @ScieceSek			nvarchar(20) = case (select wartosc from @Kl25000 where escode=1004) when 40 then 25 else 60 end;
-------------
declare @DLUGOSC			nvarchar(20) = (select wartosc from @Kl25000 where escode=1) + case (@K2_I) when '1' then 30 else 20 end; -- czy korygujemy o grubość OKB x 1
declare @WYSOKOSC			nvarchar(20) = '610'; --- do dodania zmienna w klasie 25200 oraz 25220
declare @PODW_ZAW			nvarchar(20) = (select wartosc from @Kl25000 where escode=1742); 
declare @OMEGI_UKL			nvarchar(20) = (select wartosc from @Kl25000 where escode=1736); 
declare @H_SEK				nvarchar(20) = (select wartosc from @Kl25000 where escode=3030+@nrsek); 
declare @LICZBA_OKIEN_IP	nvarchar(20) = (select wartosc from @Kl25000 where escode=1390); -- czy napewno poprawna nazwa, zmienna uzywananie tylko do IP ('Ilosc_pol' 1390) 
declare @DRZWI_NA_OKNIE_IP	nvarchar(20) = (select wartosc from @Kl25000 where escode=1404); 
declare @WYMIAR_A			nvarchar(20) = (select wartosc from @Kl25000 where escode=3101); -- czy korygujemy o grubość OKB x1
declare @SZEROKOSC_DRZWI	nvarchar(20) = (select wartosc from @Kl25000 where escode=3102); -- moze jednak 'DS_szer_tech' czyli zamiast 3102 wez 1408 (co z korektami dla ramy na IMA)
declare @CIECIE				nvarchar(20) = iif((select wartosc from @Kl25000 where escode=3091)>0, '1', '');

declare @PROG_NAD_DS		nvarchar(20) = (select wartosc from @Kl25000 where escode=1411);
declare @LICZBA_OKIEN		nvarchar(20) = (select wartosc from @Kl25000 where escode=1304);
declare @OKNA_CZESC_A		nvarchar(20) = (select wartosc from @Kl25000 where escode=1311);
declare @OKNA_CZESC_B		nvarchar(20) = (select wartosc from @Kl25000 where escode=1313);
declare @OKNA_CZESC_D		nvarchar(20) = (select wartosc from @Kl25000 where escode=1312);
declare @Okno_typ			nvarchar(20) = (select wartosc from @Kl25000 where escode=1302);
declare @Ilosc_sekcji		nvarchar(20) = (select wartosc from @Kl25000 where escode=3010);
declare @RZEDOW_KRATEK		nvarchar(20) = (select wartosc from @Kl25000 where escode=case @nrsek when 1 then 1455 when @Ilosc_sekcji then 1458 else '' end);
declare @LICZBA_KRATEK		nvarchar(20) = (select wartosc from @Kl25000 where escode=case @nrsek when 1 then 1456 when @Ilosc_sekcji then 1456 else '' end);
declare @UCHWYT				nvarchar(20) = (select wartosc from @Kl25000 where escode=1701 and @nrsek=1);
declare @ZAMEK				nvarchar(20) = (select wartosc from @Kl25000 where escode=1702 and @nrsek=2);
declare @WzmZawBoc			nvarchar(20) = (select wartosc from @Kl25000 where escode=1665 and @nrsek<>@Ilosc_sekcji);
-----------------------------------------------------------------------------------------------------------------------------------------------------------