repo.IndexColumn_ReferencedReferencing_HasFullColumnsInReferencing - V

type: V ( view ), modify_date: 2022-01-05 18:06:33

RepoObject_guid: 7B90291C-9D61-EB11-84DC-A81E8446D5B0

Description

Examples

Entity Diagram

entity-repo.indexcolumn_referencedreferencing_hasfullcolumnsinreferencing

Columns

Table 1. Columns of repo.IndexColumn_ReferencedReferencing_HasFullColumnsInReferencing - V
PK Column Name Data Type NULL? ID

1

uniqueidentifier

NOT NULL

2

int

NOT NULL

3

bigint

NULL

int

NULL

bit

NULL

uniqueidentifier

NULL

uniqueidentifier

NOT NULL

uniqueidentifier

NULL

uniqueidentifier

NOT NULL

Foreign Key Diagram

entity_1_1_fk-repo.indexcolumn_referencedreferencing_hasfullcolumnsinreferencing

References

Object Reference Diagram - 1 1

entity_1_1_objectref-repo.indexcolumn_referencedreferencing_hasfullcolumnsinreferencing

Object Reference Diagram - Referenced - 30 0

entity_30_0_objectref-repo.indexcolumn_referencedreferencing_hasfullcolumnsinreferencing

Object Reference Diagram - Referencing - 0 30

entity_0_30_objectref-repo.indexcolumn_referencedreferencing_hasfullcolumnsinreferencing

Column Reference Diagram

entity_1_1_colref-repo.indexcolumn_referencedreferencing_hasfullcolumnsinreferencing

Indexes

PK_IndexColumn_ReferencedReferencing_HasFullColumnsInReferencing

idx_IndexColumn_ReferencedReferencing_HasFullColumnsInReferencing__2

Column Details

_

index_guid

1

index_guid

uniqueidentifier

NOT NULL

index_column_id

2

index_column_id

int

NOT NULL

RowNumberInReferencing

3

RowNumberInReferencing

bigint

NULL

ColumnsPerIndex

ColumnsPerIndex

int

NULL

is_descending_key

is_descending_key

bit

NULL

referenced_RepoObject_guid

referenced_RepoObject_guid

uniqueidentifier

NULL

referenced_RepoObjectColumn_guid

referenced_RepoObjectColumn_guid

uniqueidentifier

NOT NULL

referencing_RepoObject_guid

referencing_RepoObject_guid

uniqueidentifier

NULL

referencing_RepoObjectColumn_guid

referencing_RepoObjectColumn_guid

uniqueidentifier

NOT NULL

sql_modules_definition

repo.IndexColumn_ReferencedReferencing_HasFullColumnsInReferencing - V script
/*
we use all Index from repo.IndexColumn__union (real and virtual)
and we use Object references
- currently from [repo].[RepoObjectColumn_reference_union] AS ref
  but mabe this should be changed

so we get all possible index inheritence into any referencing object

first condition: any column of the referenced index should be a referencing column in the referencing object

second condition: all columns of the referenced index should be a referencing column in the referencing object
this means: "HasFullColumnsInReferencing"


Attention:
a source object can be joined several times
=> a source index can have several target index in the same target object
=> we implement [RowNumberInReferencing]

SELECT
       [index_guid]
     , [index_column_id]
     , [RepoObjectColumn_guid]
     , [referenced_RepoObject_guid]
     , [referenced_RepoObjectColumn_guid]
     , [referencing_RepoObject_guid]
     , COUNT(*) AS [Anz]
FROM
     repo.IndexColumn_ReferencedReferencing_HasFullColumnsInReferencing AS T1
GROUP BY
         [index_guid]
       , [index_column_id]
       , [RepoObjectColumn_guid]
       , [referenced_RepoObject_guid]
       , [referenced_RepoObjectColumn_guid]
       , [referencing_RepoObject_guid]
ORDER BY
         [Anz] DESC


*/
CREATE View repo.IndexColumn_ReferencedReferencing_HasFullColumnsInReferencing
As
--
--referencing columns, that inherit an index
--the goal of this view is to include only RepoObjects, which inherit ALL referenced columns of a source index
Select
    --[index_guid] is referenced index, [index_column_id] is the column of a referenced index
    ic.index_guid
  , ic.index_column_id
  , ic.is_descending_key
  , ic.ColumnsPerIndex
                                         --, [ic].[RepoObjectColumn_guid] --referenced RepoObjectColumn; redundant; remove it, if not used
  , ref.referenced_RepoObject_guid
  , ref.referenced_RepoObjectColumn_guid --also referenced RepoObjectColumn (as above), left join; but is it possible that it is NULL? No, because of the first EXISTS condition
  , ref.referencing_RepoObject_guid
  , ref.referencing_RepoObjectColumn_guid
                                         --the same index can be inherited several times into the same referenced object, if a source is used several times
                                         --for example
                                         --SELECT A_A = A.A, B_A = B.A from source_1 as A LEFT JOIN source_1 as B ON ...
                                         --normaly these indexes should have different columns
  , RowNumberInReferencing = Row_Number () Over ( Partition By
                                                      --soure index column
                                                      ic.index_guid
                                                    , ic.index_column_id
                                                    , ref.referenced_RepoObject_guid
                                                    , ref.referenced_RepoObjectColumn_guid
                                                    --taget index, if the same source index column is inherited several times into a target object
                                                    , ref.referencing_RepoObject_guid
                                                  Order By
                                                      ref.referenced_RepoObjectColumn_guid
                                                )
--, roc.[RepoObjectColumn_guid]
From
    repo.IndexColumn_union_T                   As ic
    Inner Join --todo: maybe use another source for RepoObject references
        --repo.[RepoObjectColumn_reference_FirstResultSet] AS ref
        reference.RepoObjectColumn_reference_T As ref
            On
            ref.referenced_RepoObjectColumn_guid = ic.RepoObjectColumn_guid
Where
    --first condition: any column of the referenced index should be a referencing column in the referencing objekt
    Exists
(
    Select
        roc.RepoObject_guid
    From
        repo.RepoObjectColumn As roc
    Where
        --[RepoObject_guid] is matching
        --=> some source columns (referenced) are referenced in the target (referencing)
        --but it is possible, that not all source columns are referenced
        --to exclude these RepoObject with incomlete inheritance, the next condition will remove these RepoObject
        roc.RepoObject_guid = ref.referencing_RepoObject_guid
)
    And Not Exists
--second condition: all columns of the referenced index should be a referencing column in the referencing object
--this means: "HasFullColumnsInReferencing"
(
    Select
        roc.RepoObject_guid
    From
        repo.RepoObjectColumn As roc
    Where
        --[RepoObject_guid] is matching
        roc.RepoObject_guid = ref.referencing_RepoObject_guid
        --but some referencing columns are missing in referencing object
        --this is possible in case of a composed key where not all columns of the source are referenced in the target
        And ref.referencing_RepoObjectColumn_guid Is Null
)