我在下面编写了SQL代码以返回以下结果集。此代码允许用户选择DataLibrary(Parent)并查找其基础DataLibraries(子)。
Classification || DLGroup || DataLibraryName || Usedby || EntityName || SubEntityName
InUse || MO || SingleorAll || MDL || DashboardA ||
InUse || MO || SingleorAll || MDL || DashboardB ||
InUse || MO || SingleorAll || MDL || DashboardC ||
-- Begin Code --
SET NOCOUNT ON
DECLARE @SelectedDL VARCHAR(50)
SET @SelectedDL = '1264'
--BEGIN TRY
--CREATE CLUSTERED INDEX [ClusteredIndex-20130828-063538] ON [dbo].[tblrpt_view]
--(
-- [view_type_id] ASC
--)
--END TRY
--BEGIN CATCH
-- ALREADY THERE
--END CATCH
DECLARE @PPTReports TABLE (
rpt_id INTEGER
)
INSERT INTO @PPTReports
SELECT RPT.rpt_id
FROM dbo.tblrpt_main RPT
JOIN dbo.tblrpt_view V
ON V.rpt_id = RPT.rpt_id
AND V.view_type_id = 1999
IF IsNull(@SelectedDL,'') = '' SET @SelectedDL = '0'
SELECT 'In Use' as Classification,
DLG.group_name as DLGroup,
DL.datasource_name as DataLibraryName,
'Report' as UsedBy,
R.rpt_name as EntityName,
CONVERT(VARCHAR(255),'') AS SubEntityName
FROM dbo.tblrpt_datascript DL
JOIN dbo.tblrpt_datascript_group DLG
ON DLG.d_group_id = DL.datascript_group
JOIN dbo.tblrpt_main R
ON R.rpt_data_source = DL.datasource_id
WHERE (DL.datasource_id = @SelectedDL
OR @SelectedDL = '0')
UNION
SELECT 'In Use' as Classification,
DLG.group_name,
DL.datasource_name as DataLibraryName,
'Legacy Chart' as Category,
R.rpt_name as EntityName,
C.control_name as SubEntityName
FROM dbo.tblrpt_datascript DL
JOIN dbo.tblrpt_datascript_group DLG
ON DLG.d_group_id = DL.datascript_group
JOIN dbo.tblrpt_chart C
ON C.datasource_id = DL.datasource_id
JOIN dbo.tblrpt_main R
ON R.rpt_id = C.rpt_id
WHERE (DL.datasource_id = @SelectedDL
OR @SelectedDL = '0')
UNION
SELECT 'In Use' as Classification,
DLG.group_name,
DL.datasource_name as DataLibraryName,
'Merge Data Library' as Category,
MDL.datasource_name as EntityName,
'' as SubEntityName
FROM dbo.tblrpt_datascript DL
JOIN dbo.tblrpt_datascript_group DLG
ON DLG.d_group_id = DL.datascript_group
JOIN dbo.tblrpt_datascript MDL
ON MDL.datalibraryXML LIKE '%<DataLibaryId>'+ CONVERT(VARCHAR,DL.datasource_id)+'</DataLibaryId>%'
WHERE (DL.datasource_id = @SelectedDL
OR @SelectedDL = '0')
UNION
SELECT 'In Use' as Classification,
DLG.group_name,
DL.datasource_name as DataLibraryName,
'Prevalidation' as Category,
PV.name as EntityName,
'' as SubEntityName
FROM dbo.tblrpt_datascript DL
JOIN dbo.tblrpt_datascript_group DLG
ON DLG.d_group_id = DL.datascript_group
JOIN dbo.prevalidators PV
ON PV.datasourceId = DL.datasource_id
WHERE (DL.datasource_id = @SelectedDL
OR @SelectedDL = '0')
UNION
SELECT 'In Use' as Classification,
DLG.group_name,
DL.datasource_name as DataLibraryName,
'Matrix' as Category,
WFQ.data_event_name as EntityName,
'' as SubEntityName
FROM dbo.tblrpt_datascript DL
JOIN dbo.tblrpt_datascript_group DLG
ON DLG.d_group_id = DL.datascript_group
JOIN dbo.workflow_data_events WFQ
ON WFQ.datasource_id = DL.datasource_id
WHERE (DL.datasource_id = @SelectedDL
OR @SelectedDL = '0')
UNION
SELECT 'In Use' as Classification,
DLG.group_name,
DL.datasource_name as DataLibraryName,
'Commentary Template' as Category,
COM.template_name as EntityName,
'' as SubEntityName
FROM dbo.tblrpt_datascript DL
JOIN dbo.tblrpt_datascript_group DLG
ON DLG.d_group_id = DL.datascript_group
JOIN dbo.commentary_template COM
ON COM.datalibrary_id = DL.datasource_id
WHERE (DL.datasource_id = @SelectedDL
OR @SelectedDL = '0')
UNION
SELECT 'In Use' as Classification,
DLG.group_name,
DL.datasource_name as DataLibraryName,
'Financial Control' as Category,
C.name as EntityName,
'' as SubEntityName
FROM dbo.tblrpt_datascript DL
JOIN dbo.tblrpt_datascript_group DLG
ON DLG.d_group_id = DL.datascript_group
JOIN dbo.financial_controls_data_source DS
ON DS.datasource_id = DL.datasource_id
JOIN dbo.financial_controls C
ON C.id = DS.financial_control_id
WHERE (DL.datasource_id = @SelectedDL
OR @SelectedDL = '0')
UNION
SELECT 'In Use' as Classification,
DLG.group_name,
DL.datasource_name as DataLibraryName,
'PPT Child DL' as Category,
RPT.rpt_name as EntityName,
'' as SubEntityName
FROM @PPTReports TMP
JOIN dbo.tblrpt_main RPT
ON RPT.rpt_id = TMP.rpt_id
JOIN dbo.tblrpt_view V
ON V.rpt_id = RPT.rpt_id
JOIN dbo.tblrpt_datascript DL
ON DL.datasource_id != RPT.rpt_data_source
AND SUBSTRING(V.custom_mechanism_xml,1,2000) LIKE '%<DlName>'+ DL.datasource_name+'</DlName>%'
JOIN dbo.tblrpt_datascript_group DLG
ON DLG.d_group_id = DL.datascript_group
WHERE
(DL.datasource_id = @SelectedDL
OR @SelectedDL = '0')
UNION
SELECT 'NOT Used',
DLG.group_name,
DL.datasource_name,
'' as Category,
'' as EntityName,
'' as SubEntityName
FROM dbo.tblrpt_datascript DL
JOIN dbo.tblrpt_datascript_group DLG
ON DLG.d_group_id = DL.datascript_group
WHERE (DL.datasource_id = @SelectedDL
OR @SelectedDL = '0')
AND NOT EXISTS (
(SELECT 1 from dbo.tblrpt_main R WHERE R.rpt_data_source = DL.datasource_id)
UNION
(SELECT 1 from dbo.financial_controls_data_source FC WHERE FC.datasource_id = DL.datasource_id)
UNION
(SELECT 1 from dbo.tblrpt_chart C WHERE C.datasource_id = DL.datasource_id)
UNION
(SELECT 1 from dbo.prevalidators PV WHERE PV.datasourceId = DL.datasource_id)
UNION
(SELECT 1 from dbo.commentary_template COM WHERE COM.datalibrary_id = DL.datasource_id)
UNION
(SELECT 1 from dbo.tblrpt_datascript MDL WHERE MDL.datalibraryXML LIKE '%<DataLibaryId>'+ CONVERT(VARCHAR,DL.datasource_id)+'</DataLibaryId>%')
UNION
(SELECT 1 from dbo.tblrpt_view VV JOIN @PPTReports PP ON PP.rpt_id = VV.rpt_id AND SUBSTRING(VV.custom_mechanism_xml,1,2000) LIKE '%<DlName>'+ DL.datasource_name+'</DlName>%')
)
ORDER BY 1,2,3,4,5,6
-- End Code --
我想更进一步,有三个级别:Parent DL,Child DL,子DL的SUB DL。我认为游标是保持循环的最佳方式,直到它找不到另一个数据库,但我不确定如何编写代码。以下是我希望数据显示的内容:
DataLibraryName || Parent
SingleOrAll || DashboardA
SingleOrAll || DashboardB
SingleOrAll || DashboardC
DashboardA || CountryA
DashboardA || CountryB
DashboardB || ContinentU
DashboardC || Atlantic1
DashboardC || Pacific3
DashboardC || China1
请帮忙