You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
123 lines
8.7 KiB
123 lines
8.7 KiB
USE [Vertragsverwaltung_20160404]
|
|
GO
|
|
/****** Object: StoredProcedure [dbo].[sp_architektur_crosstab] Script Date: 02.12.2016 09:08:54 ******/
|
|
DROP PROCEDURE [dbo].[sp_architektur_crosstab]
|
|
GO
|
|
/****** Object: StoredProcedure [dbo].[sp_architektur_crosstab] Script Date: 02.12.2016 09:08:55 ******/
|
|
SET ANSI_NULLS ON
|
|
GO
|
|
SET QUOTED_IDENTIFIER ON
|
|
GO
|
|
|
|
-- =============================================
|
|
-- Author: <Author,,Name>
|
|
-- Create date: <Create Date,,>
|
|
-- Description: <Description,,>
|
|
-- =============================================
|
|
CREATE PROCEDURE [dbo].[sp_architektur_crosstab]
|
|
@type int,
|
|
@sqlwhere varchar(1024)
|
|
AS
|
|
BEGIN
|
|
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TmpCrosstabResult]') AND type in (N'U')) DROP TABLE [dbo].[TmpCrosstabResult]
|
|
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TmpCrosstabResult1]') AND type in (N'U')) DROP TABLE [dbo].[TmpCrosstabResult1]
|
|
|
|
declare @xsql varchar(8000)
|
|
|
|
CREATE TABLE [dbo].[TmpCrosstabResult](
|
|
[KeyValue] [int] NULL
|
|
) ON [Default]
|
|
|
|
create Table #tmp1(
|
|
[Applikation] [Varchar] (255) null,
|
|
[Col] [varchar] (255) null,
|
|
[colid] int null
|
|
)
|
|
|
|
if @type=1 begin
|
|
set @xsql='SELECT DISTINCT Bezeichnung AS Applikation, REPLACE(Datenbank,'+ CHAR(39)+' ' + char(39)+', '+CHAR(39)+'_'+CHAR(39)+') AS Col, 0 as ColID FROM dbo.View_Applikation_Server '
|
|
if @sqlwhere <> '' begin
|
|
set @xsql=@xsql+' where ' + @sqlwhere + ' '
|
|
end
|
|
insert into #tmp1 exec(@xsql)
|
|
end
|
|
if @type=2 begin
|
|
set @xsql='SELECT DISTINCT Bezeichnung AS Applikation, REPLACE(Datenbank,'+ CHAR(39)+' ' + char(39)+', '+CHAR(39)+'_'+CHAR(39)+') AS Col, 0 as ColID FROM dbo.View_Applikation_Sst '
|
|
if @sqlwhere <> '' begin
|
|
set @xsql=@xsql+' where ' + @sqlwhere + ' '
|
|
end
|
|
insert into #tmp1 exec(@xsql)
|
|
end
|
|
if @type=3 begin
|
|
set @xsql='SELECT DISTINCT Bezeichnung AS Applikation, REPLACE(Datenbank,'+ CHAR(39)+' ' + char(39)+', '+CHAR(39)+'_'+CHAR(39)+') AS Col, 0 as ColID FROM dbo.View_Applikation_Datenbank '
|
|
if @sqlwhere <> '' begin
|
|
set @xsql=@xsql+' where ' + @sqlwhere + ' '
|
|
end
|
|
insert into #tmp1 exec(@xsql)
|
|
end
|
|
|
|
|
|
declare @col varchar(255)
|
|
declare @cnt int
|
|
set @cnt=0
|
|
declare xc cursor for
|
|
select distinct col from #tmp1
|
|
open xc
|
|
fetch next from xc into @col
|
|
while @@FETCH_STATUS=0 begin
|
|
update #tmp1 set colid=@cnt where COL=@col
|
|
set @cnt=@cnt+1
|
|
fetch next from xc into @col
|
|
end
|
|
close xc
|
|
deallocate xc
|
|
|
|
exec sys_crosstab #tmp1, 'colid','col','Col',1,'Applikation', @CalcOperation='max',@debug=0,@TempTableName='TmpCrosstabResult'
|
|
|
|
|
|
CREATE TABLE [dbo].[TmpCrosstabResult1](
|
|
[KeyValue] [int] NULL,
|
|
[Applikation] [varchar] (255) null
|
|
) ON [Default]
|
|
|
|
declare @column varchar(255)
|
|
set @xsql = ''
|
|
set @cnt=0
|
|
declare xx cursor for
|
|
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'TmpCrosstabResult' and column_name <> 'KeyValue' and column_name <> 'Applikation'
|
|
open xx
|
|
fetch next from xx into @column
|
|
while @@FETCH_STATUS=0 begin
|
|
set @xsql = 'alter table dbo.tmpcrosstabresult1 add ['+@column+'] varchar(1) null'
|
|
exec(@xsql)
|
|
fetch next from xx into @column
|
|
end
|
|
close xx
|
|
deallocate xx
|
|
|
|
|
|
insert into TmpCrosstabResult1 select * from TmpCrosstabResult
|
|
exec('alter table tmpcrosstabresult1 drop column keyvalue')
|
|
|
|
declare xx cursor for
|
|
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'TmpCrosstabResult1' and column_name <> 'Applikation'
|
|
open xx
|
|
fetch next from xx into @column
|
|
while @@FETCH_STATUS=0 begin
|
|
set @xsql = 'update tmpcrosstabresult1 set ['+@column+']='+char(39)+'X'+char(39) +' where ['+@column+'] <> '+char(39)+'0'+char(39)
|
|
exec(@xsql)
|
|
set @xsql = 'update tmpcrosstabresult1 set ['+@column+']='+char(39)+''+char(39) +' where ['+@column+'] = '+char(39)+'0'+char(39)
|
|
exec(@xsql)
|
|
fetch next from xx into @column
|
|
end
|
|
close xx
|
|
deallocate xx
|
|
|
|
select * from TmpCrosstabResult1
|
|
drop table tmpcrosstabresult
|
|
drop table tmpcrosstabresult1
|
|
END
|
|
|
|
|
|
GO
|