MASZ POMYSŁ MA OPTYMALIZACJĘ KODU:
declare
@aufnr int = 824814
,@posnr int = 5
,@StrMoc char(1) = 'L'
,@nrsek int = 3
;
DECLARE @Kl25000 TABLE (artklcode int, escode int, wartosc nvarchar(255));
INSERT INTO @Kl25000
SELECT * FROM dbo.fn_brseg_ImaKl_25000(@aufnr, @posnr, 25000, '')
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),PozOsY nvarchar(20));
INSERT INTO @Kl25304_1206
SELECT * FROM (SELECT * 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);
SET @Okno_umiejsc = REPLACE(@Okno_umiejsc, '-', '.');
declare @OknoWykonaj int ;
SET @OknoWykonaj = IIF(
CHARINDEX('.', @Okno_umiejsc, 1) > 0,
IIF(
@nrsek BETWEEN CAST(PARSENAME(REVERSE(@Okno_umiejsc), 1) AS INT) AND CAST(PARSENAME(REVERSE(@Okno_umiejsc), 2) AS INT),
1,
0
),
IIF(CAST(@Okno_umiejsc AS INT)=@nrsek, 1, 0)
);
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) = case(select wartosc from @Kl25000 where escode=1742) when 'J' then '1' else '0' end;
declare @OMEGI_UKL nvarchar(20) = case(select wartosc from @Kl25000 where escode=1736) when '1' then '1' when '2' then '2' else '0' end;
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_DS nvarchar(20) = (select wartosc from @Kl25000 where escode=1403);
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 @KraWen nvarchar(20) = (select wartosc from @Kl25000 where escode=1793);
declare @KW_lok nvarchar(20) = (select wartosc from @Kl25000 where escode=1457);
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 1459 else '' end);
declare @KRATEK_OBSZAR_A nvarchar(20) = iif(@DS='J' and @KW_lok like '%D%',(select wartosc from @Kl25000 where escode=1462),'');
declare @KRATEK_OBSZAR_D nvarchar(20) = iif(@DS='J' and @KW_lok like '%D%',(select wartosc from @Kl25000 where escode=1463),'');
declare @KRATEK_OBSZAR_B nvarchar(20) = iif(@DS='J' and @KW_lok like '%D%',(select wartosc from @Kl25000 where escode=1464),'');
select *
from(
(select Id=1, Parametry from IMAWOP11_PARAMETRY_v2 where SORT=1)
union all (select Id=2, @DLUGOSC from IMAWOP11_PARAMETRY_v2 where SORT=1)
union all (select Id=3, Parametry from IMAWOP11_PARAMETRY_v2 where SORT=2)
union all (select Id=4, '635' from IMAWOP11_PARAMETRY_v2 where SORT=2)
union all (select top 1 Id=5, Parametry from IMAWOP11_PARAMETRY_v2 where SORT=3)
union all (select top 1 Id=6, @PODW_ZAW from IMAWOP11_PARAMETRY_v2 where SORT=3)
union all (select top 1 Id=7, Parametry from IMAWOP11_PARAMETRY_v2 where SORT=4)
union all (select top 1 Id=8, @OMEGI_UKL from IMAWOP11_PARAMETRY_v2 where SORT=4)
union all (select Id=9, Parametry from IMAWOP11_PARAMETRY_v2 where NrSek=@nrsek and SORT=5)
union all (select Id=10, @H_SEK from IMAWOP11_PARAMETRY_v2 where NrSek=@nrsek and SORT=5)
union all (select top 1 Id=11, Parametry from IMAWOP11_PARAMETRY_v2 where SORT=6)
union all (select top 1 Id=12, @K2_I from IMAWOP11_PARAMETRY_v2 where SORT=6)
union all (select Id=13, iif(@LICZBA_OKIEN_IP>0, Parametry, '') from IMAWOP11_PARAMETRY_v2 where SORT=7)
union all (select Id=14, iif(@LICZBA_OKIEN_IP>0, @LICZBA_OKIEN_IP, '') from IMAWOP11_PARAMETRY_v2 where SORT=7)
union all (select Id=15, iif(@DRZWI_NA_OKNIE_IP like '%z%' and @DS='1', Parametry, '') from IMAWOP11_PARAMETRY_v2 where SORT=8)
union all (select Id=16, iif(@DRZWI_NA_OKNIE_IP like '%z%' and @DS='1', @DRZWI_NA_OKNIE_IP, '') from IMAWOP11_PARAMETRY_v2 where SORT=8)
union all (select Id=17, iif(@DS='1', Parametry, '') from IMAWOP11_PARAMETRY_v2 where SORT=9)
union all (select Id=18, iif(@DS='1', @WYMIAR_A, '') from IMAWOP11_PARAMETRY_v2 where SORT=9)
union all (select Id=19, iif(@DS='1', Parametry, '') from IMAWOP11_PARAMETRY_v2 where SORT=10)
union all (select Id=20, iif(@DS='1', @SZEROKOSC_DRZWI, '') from IMAWOP11_PARAMETRY_v2 where SORT=10)
union all (select Id=21, iif(@DS='1' and @Ostatnia_DS='1' and Rodzaj_sekcji=@Rodzaj_sekcji and @CIECIE=1, Parametry, '') from IMAWOP11_PARAMETRY_v2 where SORT=11)
union all (select Id=22, iif(@DS='1' and @Ostatnia_DS='1' and Rodzaj_sekcji=@Rodzaj_sekcji and @CIECIE=1, @CIECIE, '') from IMAWOP11_PARAMETRY_v2 where SORT=11)
union all (select Id=23, iif(@DS='1' and NrSek=1 and Rodzaj_sekcji=@Rodzaj_sekcji, Parametry, '') from IMAWOP11_PARAMETRY_v2 where SORT=12)
union all (select Id=24, iif(@DS='1' and NrSek=1 and Rodzaj_sekcji=@Rodzaj_sekcji, @PROG_DS, '') from IMAWOP11_PARAMETRY_v2 where SORT=12)
union all (select Id=25, iif(@DS='1' and @Ostatnia_DS='1' and Rodzaj_sekcji=@Rodzaj_sekcji, Parametry, '') from IMAWOP11_PARAMETRY_v2 where SORT=13)
union all (select Id=26, iif(@DS='1' and @Ostatnia_DS='1' and Rodzaj_sekcji=@Rodzaj_sekcji, @PROG_NAD_DS, '') from IMAWOP11_PARAMETRY_v2 where SORT=13)
--- OKNA --------------------------------------------------------------------------------------------------------------------------------------------------------
union all (select Id=101, iif(@OknoWykonaj>0 and @LICZBA_OKIEN>0 and Aktywne=1, Parametry, '') from IMAWOP11_PARAMETRY_v2 where SORT=101)
union all (select Id=102, iif(@OknoWykonaj>0 and @LICZBA_OKIEN>0 and Aktywne=1, @LICZBA_OKIEN, '') from IMAWOP11_PARAMETRY_v2 where SORT=101)
union all (select Id=103, iif(@OknoWykonaj>0 and @OKNA_CZESC_A>0 and Aktywne=1, Parametry, '') from IMAWOP11_PARAMETRY_v2 where SORT=102)
union all (select Id=104, iif(@OknoWykonaj>0 and @OKNA_CZESC_A>0 and Aktywne=1, @OKNA_CZESC_A, '') from IMAWOP11_PARAMETRY_v2 where SORT=102)
union all (select Id=105, iif(@OknoWykonaj>0 and @OKNA_CZESC_D>0 and Aktywne=1, Parametry, '') from IMAWOP11_PARAMETRY_v2 where SORT=103)
union all (select Id=106, iif(@OknoWykonaj>0 and @OKNA_CZESC_D>0 and Aktywne=1, @OKNA_CZESC_D, '') from IMAWOP11_PARAMETRY_v2 where SORT=103)
union all (select Id=107, iif(@OknoWykonaj>0 and @OKNA_CZESC_B>0 and Aktywne=1, Parametry, '') from IMAWOP11_PARAMETRY_v2 where SORT=104)
union all (select Id=108, iif(@OknoWykonaj>0 and @OKNA_CZESC_B>0 and Aktywne=1, @OKNA_CZESC_B, '') from IMAWOP11_PARAMETRY_v2 where SORT=104)
union all (select Id=109, iif(@OknoWykonaj>0 and @LICZBA_OKIEN>0 and Aktywne=1, Parametry, '') from IMAWOP11_PARAMETRY_v2 where SORT=105)
union all (select Id=110, PozOsY from @Kl25304_1206 where @OknoWykonaj>0 and @Okno_typ=ARTNR)
--- KRATKI ------------------------------------------------------------------------------------------------------------------------------------------------------
--- union all (select Id=110, iif(@LICZBA_OKIEN>0 and Aktywne=1, @LICZBA_OKIEN, '') from IMAWOP11_PARAMETRY_v2 where SORT=101)
) t
where Parametry<>''
order by Id