USE [Vertragsverwaltung_20160404] GO /****** Object: StoredProcedure [dbo].[sp_get_applstruktur] Script Date: 02.12.2016 09:08:53 ******/ DROP PROCEDURE [dbo].[sp_get_applstruktur] GO /****** Object: StoredProcedure [dbo].[sp_get_applstruktur] Script Date: 02.12.2016 09:08:55 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO -- ============================================= -- Author: -- Create date: <13.01.2008> -- Description: -- ============================================= CREATE PROCEDURE [dbo].[sp_get_applstruktur] @mitarbeiternr int, @nuraktive int=0, @alphasort int=0 AS --exec sp_get_applstruktur_test @mitarbeiternr, @nuraktive,@alphasort --return CREATE TABLE #tmpd( [applikationsnr] [int] , [bezeichnung] [varchar] (255) null, [parentid] [int] null, [SecurityLevelNr] [int] null, [kategorienr] [int] null, [lic] bit null, [aktiv] [bit] null, [Strukturelement] [bit] null, ) ON [DEFAULT] -- Rootentry für die Treeaufbereitung in der Applikation insert #tmpd (applikationsnr, bezeichnung, parentid, aktiv) values(0,'Root',null,1) declare @appnr int declare @appbez varchar(50) declare @appprnt int declare @anr int declare @bez varchar(50) declare @pid int declare @snr int declare @aktiv bit declare @knr int set @appprnt = -1 ----------------------------------------------------------------------------------------- -- Erster Loop über die Applikationen, welche den entsprechenden Suchbegriff beinhalten ----------------------------------------------------------------------------------------- SELECT DISTINCT dbo.Applikation.ApplikationNr, dbo.Applikation.Bezeichnung, dbo.Applikation.ParentID, dbo.Applikation.SecurityLevelNr, dbo.Applikation.Aktiv into #tmpx FROM dbo.Applikation if @nuraktive=1 begin delete from #tmpx where aktiv<>1 end if @nuraktive=0 begin delete from #tmpx where aktiv<>0 end declare xc cursor for select * from #tmpx open xc fetch next from xc into @appnr, @appbez, @pid, @snr, @aktiv while @@fetch_status=0 begin ----------------------------------------------------------------------------------------- -- Zweiter Loop über die Einträge der obesten Ebene der Applikationen ----------------------------------------------------------------------------------------- declare yc cursor for select applikationnr, bezeichnung, parentid, securitylevelnr, applikationkategorienr, aktiv from applikation where applikationnr=@appnr open yc fetch next from yc into @anr, @bez, @pid, @snr, @knr, @aktiv while @@fetch_status=0 begin insert #tmpd(applikationsnr, bezeichnung, parentid, securitylevelnr, kategorienr, aktiv) values (@anr, @bez, @appprnt, @snr, @knr, @aktiv) ----------------------------------------------------------------------------------------- -- Je Applikation die untergeordneten Applikationen/Module auslesen ----------------------------------------------------------------------------------------- --execute dbo.sp_get_applstruktur_down @anr,1, @anr execute dbo.sp_get_applstruktur_up @anr,1 fetch next from yc into @anr, @bez, @pid, @snr, @knr, @aktiv end close yc deallocate yc set @appprnt = @appprnt - 1 fetch next from xc into @appnr, @appbez, @pid, @snr, @aktiv end close xc deallocate xc update #tmpd set kategorienr = (select applikationkategorienr*-1 from applikation where applikationnr=#tmpd.applikationsnr and #tmpd.parentid=0) update #tmpd set parentid=kategorienr where kategorienr < 0 declare kc cursor for SELECT distinct dbo.ApplikationKategorie.ApplikationKategorieNr, dbo.ApplikationKategorie.Bezeichnung FROM dbo.Applikation inner join dbo.ApplikationKategorie ON dbo.Applikation.ApplikationKategorieNr = dbo.ApplikationKategorie.ApplikationKategorieNr inner join dbo.#tmpd ON dbo.Applikation.ApplikationNr = dbo.#tmpd.applikationsnr open kc fetch next from kc into @knr, @bez while @@fetch_status=0 begin insert #tmpd(applikationsnr, bezeichnung, parentid, securitylevelnr, aktiv) values (@knr * -1, @bez, 0, 0, @aktiv) fetch next from kc into @knr, @bez end close kc deallocate kc ----------------------------------------------------------------------------------------- -- Daten zurück geben und temporäre Tabelle löschen ----------------------------------------------------------------------------------------- delete from #tmpd where kategorienr is null and parentid < 0 or applikationsnr is null insert #tmpd (applikationsnr, bezeichnung, parentid, aktiv) values(0,'Root',null,1) update #tmpd set lic=0, Strukturelement=0 update #tmpd set lic=NurLizenz, Strukturelement = applikation.strukturelement from applikation where #tmpd.applikationsnr=applikation.applikationnr if @alphasort = 1 begin update #tmpd set parentid=0 where parentid<0 delete from #tmpd where applikationsnr < 0 if @nuraktive=0 begin select distinct applikationsnr, bezeichnung, parentid, lic, aktiv, strukturelement,'' as Treffer from #tmpd where ltrim(bezeichnung) <> '' order by bezeichnung end else begin select distinct applikationsnr, bezeichnung, parentid, lic, aktiv, strukturelement,'' as Treffer from #tmpd where ltrim(bezeichnung) <> '' order by bezeichnung end drop table #tmpd return end if @nuraktive=0 begin select distinct applikationsnr, bezeichnung, parentid, lic, aktiv, strukturelement from #tmpd where ltrim(bezeichnung) <> '' order by bezeichnung end else begin select distinct applikationsnr, bezeichnung, parentid, lic, aktiv, strukturelement from #tmpd where ltrim(bezeichnung) <> '' order by bezeichnung end drop table #tmpd GO