Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 10.69 KB | None | 0 0
  1. USE [SDS_DevSchoolDistrict]
  2. GO
  3.  
  4. --SELECT * FROM MetadataOfTableFields('AssessmentTestPerformanceLevels')
  5.  
  6. GO
  7. ALTER FUNCTION [dbo].[MetadataOfTableFields]
  8. (@TableName sysname)
  9. --DECLARE @TableName sysname        SET @TableName = 'Classes'
  10. RETURNS
  11. --DECLARE
  12. @MetadataOfTableFields TABLE
  13. (       ------------------------------------------
  14.         FieldName               sysname,
  15.         FieldOrdinal            int,
  16.         FieldDataType           sysname,
  17.         FieldValueType          sysname,
  18.         FieldInterfaceType      sysname,
  19.    
  20.         FieldMaximumLength      int,
  21.         FieldIsNumeric          bit,
  22.         FieldIsRequired         bit,
  23.         FieldIsUnique           bit,
  24.         FieldRequiresSelection  bit,
  25.         FieldConstraint         varchar(500),
  26.  
  27.         FieldIsPrimaryKey       bit,
  28.         FieldIsIdentity         bit,
  29.         FieldIsRepresentative   bit,
  30.         FieldIsForeignKey       bit,
  31.         FieldIsNullable         bit,
  32.         FieldIsComputed         bit,
  33.         FieldIsUpdateable       bit,
  34.         FieldFormula            varchar(500),
  35.         FieldDefaultValue       varchar(500),
  36.  
  37.         --FieldIsRelativelyUnique   bit DEFAULT 0,
  38.         UniquenessBasisField    sysname NULL DEFAULT(''),
  39.         OrdinalBasisField       sysname NULL DEFAULT(''),
  40.  
  41.         FieldSelectionSource    sysname,
  42.         FieldSelectionIdField   sysname,
  43.         FieldSelectionDisplayField  sysname
  44.  
  45. )       ------------------------------------------
  46. AS
  47. BEGIN
  48.  
  49.  
  50.         ---------------------------------------------
  51.         DECLARE @PrimaryKeyFields TABLE
  52.         (       -------------------------------------
  53.                 PrimaryKeyFieldName sysname
  54.         )       -------------------------------------
  55.         ---------------------------------------------
  56.         INSERT  @PrimaryKeyFields
  57.         SELECT  --i.name AS IndexName,
  58.                 --OBJECT_NAME(ic.OBJECT_ID) AS TableName,
  59.                 COL_NAME(ic.OBJECT_ID,ic.column_id) AS ColumnName
  60.         FROM    sys.indexes AS i
  61.         JOIN    sys.index_columns AS ic
  62.           ON    i.OBJECT_ID = ic.OBJECT_ID
  63.          AND    i.index_id = ic.index_id
  64.          AND    i.is_primary_key = 1
  65.          AND    i.OBJECT_ID = OBJECT_ID(@TableName)
  66.         ---------------------------------------------
  67.         -- SELECT * FROM @PrimaryKeyFields
  68.         ---------------------------------------------
  69.  
  70.  
  71.  
  72.         ---------------------------------------------
  73.         DECLARE @UniqueFields TABLE
  74.         (       -------------------------------------
  75.                 UniqueFieldName sysname
  76.         )       -------------------------------------
  77.         ---------------------------------------------
  78.         INSERT  @UniqueFields
  79.         SELECT  Fields.name
  80.         FROM    dbo.sysobjects Objects
  81.         JOIN    dbo.syscolumns Fields
  82.           ON    Objects.id = Fields.id
  83.          AND    Objects.name = @TableName
  84.         JOIN    sysconstraints Constraints
  85.           ON    Fields.Id = Constraints.id -- Constraints.constid
  86.         JOIN    sysobjects ConstraintObjects
  87.           ON    ConstraintObjects.id = Constraints.constid
  88.          AND    ConstraintObjects.xtype IN ('PK', 'UQ')
  89.         JOIN    sysindexes Indexes
  90.           ON    Indexes.id = Fields.Id
  91.          AND    ConstraintObjects.name = Indexes.name
  92.         JOIN    sysindexkeys IndexKeys
  93.           ON    IndexKeys.id = Indexes.id
  94.          AND    IndexKeys.indid = Indexes.indid
  95.          AND    Fields.colid = IndexKeys.colid
  96.         ---------------------------------------------
  97.         -- SELECT * FROM @UniqueFields
  98.         ---------------------------------------------
  99.  
  100.  
  101.  
  102.  
  103.         ---------------------------------------------
  104.         DECLARE @CheckConstraintFields TABLE
  105.         (       -------------------------------------
  106.                 CheckConstraintField    sysname,
  107.                 CheckConstraintName     sysname,
  108.                 CheckConstraint         varchar(500)
  109.         )       -------------------------------------
  110.         ---------------------------------------------
  111.         INSERT  @CheckConstraintFields
  112.         SELECT  Fields.name, Constraints.Name, Definition
  113.         FROM    sys.objects Objects
  114.         JOIN    sys.columns Fields
  115.           ON    Objects.object_id = Fields.object_id
  116.          AND    Objects.name = 'Persons'--@TableName
  117.         JOIN    sys.check_constraints Constraints
  118.           ON    Fields.object_id = Constraints.parent_object_id
  119.          AND    Fields.column_id = Constraints.parent_column_id
  120.         ---------------------------------------------
  121.         --- SELECT * FROM @CheckConstraintFields
  122.         ---------------------------------------------
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.         ---------------------------------------------
  131.         DECLARE @ForeignKeyFields TABLE
  132.         (       -------------------------------------
  133.                 ForeignKeyFieldName     sysname,
  134.                 ForeignKeyParentTable   sysname,
  135.                 ParentTableIdField      sysname,
  136.                 ParentTableDisplayField sysname
  137.         )       -------------------------------------
  138.         ---------------------------------------------
  139.         INSERT  @ForeignKeyFields
  140.         SELECT  Fields.Name,
  141.                 ParentTables.Name,
  142.                 ParentColumns.Name,
  143.                 ParentColumns2.Name
  144.         FROM    dbo.sysobjects Objects
  145.         JOIN    dbo.syscolumns Fields
  146.           ON    Objects.id = Fields.id
  147.          AND    Objects.name = @TableName
  148.         JOIN    dbo.sysforeignkeys ForeignKeys  
  149.           ON    Fields.id = ForeignKeys.fkeyid
  150.          AND    ForeignKeys.fkey = Fields.colid -- colorder
  151.          AND    OBJECTPROPERTY ( ForeignKeys.constid , 'IsForeignKey' ) = 1
  152.         JOIN    sysObjects AS ParentTables
  153.           ON    ParentTables.ID = ForeignKeys.rKeyID
  154.         JOIN    sysColumns AS ParentColumns
  155.           ON    ParentColumns.colorder = ForeignKeys.rKey
  156.          AND    ParentColumns.ID = ParentTables.ID
  157.         JOIN    sysColumns AS ParentColumns2
  158.           ON    ParentColumns2.colorder = 2
  159.          AND    ParentColumns2.ID = ParentTables.ID
  160.         ---------------------------------------------
  161.         -- SELECT * FROM @ForeignKeyFields
  162.         ---------------------------------------------
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.         ---------------------------------------------
  170.         -- Column Extended Properties
  171.         -- such as OrdinalBasisField and UniquenessBasisField
  172.         ---------------------------------------------
  173.         DECLARE @ColumnExtendedProperties TABLE
  174.         (       -------------------------------------
  175.                 FieldName       sysname,
  176.                 PropertyName    sysname,
  177.                 PropertyValue   sysname
  178.         )       -------------------------------------
  179.         ---------------------------------------------
  180.         INSERT  @ColumnExtendedProperties
  181.         SELECT  --major_id,
  182.                 --minor_id,
  183.                 --t.name AS TableName,
  184.                 c.name AS ColumnName,
  185.                 CAST(ep.name as varchar(128)) AS PropertyName,
  186.                 CAST(ep.value as varchar(128)) AS PropertyValue
  187.         FROM    sys.extended_properties AS ep
  188.         JOIN    sys.tables AS t
  189.           ON    ep.major_id = t.object_id
  190.          AND    t.name = @TableName
  191.         JOIN    sys.columns AS c
  192.           ON    ep.major_id = c.object_id
  193.          AND    ep.minor_id = c.column_id
  194.         WHERE   class = 1;
  195.         ---------------------------------------------
  196.  
  197.  
  198.  
  199.  
  200.         ---------------------------------------------
  201.         --
  202.         ---------------------------------------------
  203.         INSERT  @MetadataOfTableFields (
  204.                 FieldName,
  205.                 FieldOrdinal,
  206.                 FieldDataType,
  207.                 FieldValueType,
  208.                 FieldInterfaceType,
  209.            
  210.                 FieldMaximumLength,
  211.                 FieldIsNumeric,
  212.                 FieldIsRequired,
  213.                 FieldIsUnique,
  214.                 FieldRequiresSelection,
  215.                 FieldConstraint,
  216.  
  217.                 FieldIsPrimaryKey,
  218.                 FieldIsIdentity,
  219.                 FieldIsRepresentative,
  220.                 FieldisForeignKey,
  221.                 FieldIsNullable,
  222.                 FieldIsComputed,
  223.                 FieldIsUpdateable,
  224.                 FieldFormula,
  225.                 FieldDefaultValue,
  226.  
  227.                 FieldSelectionSource,
  228.                 FieldSelectionIdField,
  229.                 FieldSelectionDisplayField 
  230.                
  231.         )
  232.         ---------------------------------------------
  233.         SELECT  DISTINCT TOP 100 PERCENT
  234.                 Fields.name,        --AS FieldName,
  235.                 Fields.colid,       --AS FieldOrdinal,
  236.                 DataTypes.name,     --AS FieldDataType,
  237.                 ISNULL(UserDefinedDataTypes.name, ''), -- AS FieldValueType,
  238.                 dbo.InterfaceType(DataTypes.name, UserDefinedDataTypes.name, Fields.isnullable, Fields.length),-- AS InterfaceType
  239.                 Fields.length,      --AS FieldMaximumLength,
  240.                 dbo.DatatypeIsNumeric(DataTypes.name),--AS FieldIsNumeric
  241.                 CASE
  242.                     WHEN(Fields.isnullable = 1)
  243.                         THEN 0
  244.                     WHEN(Fields.iscomputed = 1)
  245.                         THEN 0
  246.                     WHEN(PK.PrimaryKeyFieldName IS NOT NULL) -- is a Primary Key
  247.                         THEN 0
  248.                     WHEN(DefaultValues.text IS NOT NULL) -- has a default
  249.                         THEN 0
  250.                     ELSE 1
  251.                 END,--AS FieldIsRequired
  252.                 CASE
  253.                     WHEN(U.UniqueFieldName IS NULL)
  254.                         THEN 0
  255.                     ELSE 1
  256.                 END,--AS FieldIsUnique
  257.                 CASE
  258.                     WHEN(FK.ForeignKeyFieldName IS NULL)
  259.                         THEN 0
  260.                     ELSE 1
  261.                 END,--AS FieldRequiresSelection,
  262.                 ISNULL(CC.CheckConstraint, ''),--AS FieldConstraint,
  263.                 CASE
  264.                     WHEN(PK.PrimaryKeyFieldName IS NULL)
  265.                         THEN 0
  266.                     ELSE 1
  267.                 END,--AS FieldIsPrimaryKey
  268.                 COLUMNPROPERTY(Fields.id, Fields.name, 'IsIdentity'), -- AS FieldIsIdentity,
  269.                 CASE
  270.                     WHEN(UserDefinedDataTypes.name = 'RepresentativeValue')
  271.                         THEN 1
  272.                     ELSE 0
  273.                 END,--FieldIsRepresentative,
  274.                 CASE
  275.                     WHEN(FK.ForeignKeyFieldName IS NULL)
  276.                         THEN 0
  277.                     ELSE 1
  278.                 END,--AS FieldisForeignKey
  279.                 Fields.isnullable--AS FieldIsNullable,
  280.                 Fields.iscomputed--AS FieldIsComputed,
  281.                 CASE
  282.                     WHEN(Fields.iscomputed = 1)
  283.                         THEN 0
  284.                     WHEN(PK.PrimaryKeyFieldName IS NOT NULL) -- is a Primary Key
  285.                         THEN 0
  286.                     ELSE 1
  287.                 END,--AS FieldIsUpdateable
  288.                 ISNULL(VirtualFields.text, ''), --AS FieldFormula,
  289.                 dbo.ExtractDefaultValue(ISNULL(DefaultValues.text, '')),--AS FieldDefaultValue
  290.                 ISNULL(FK.ForeignKeyParentTable, ''),
  291.                 ISNULL(ParentTableIdField, ''),
  292.                 ISNULL(ParentTableDisplayField, '')
  293.         ---------------------------------------------
  294.         FROM    dbo.sysobjects Objects
  295.         JOIN    dbo.syscolumns Fields
  296.           ON    Objects.id = Fields.id
  297.          AND    Objects.name = @TableName
  298.         JOIN    dbo.systypes DataTypes
  299.           ON    Fields.xtype = DataTypes.xtype
  300.          AND    DataTypes.xtype = DataTypes.xusertype
  301.         LEFT    -----------------------------------
  302.         JOIN    dbo.syscomments VirtualFields  
  303.           ON    Fields.id = VirtualFields.id
  304.          AND    VirtualFields.number = Fields.colid
  305.          AND    Fields.iscomputed = 1
  306.         LEFT    -----------------------------------
  307.         JOIN    dbo.syscomments DefaultValues  
  308.           ON    Fields.cdefault = DefaultValues.id
  309.         LEFT    -----------------------------------
  310.         JOIN    dbo.systypes UserDefinedDataTypes  
  311.           ON    Fields.usertype = UserDefinedDataTypes.xusertype
  312.  
  313.         LEFT    -----------------------------------
  314.         JOIN    @PrimaryKeyFields PK
  315.           ON    Fields.name = PK.PrimaryKeyFieldName
  316.         LEFT    -----------------------------------
  317.         JOIN    @UniqueFields U
  318.           ON    Fields.name = U.UniqueFieldName
  319.         LEFT    -----------------------------------
  320.         JOIN    @ForeignKeyFields FK
  321.           ON    Fields.name = FK.ForeignKeyFieldName
  322.         LEFT    -----------------------------------
  323.         JOIN    @CheckConstraintFields CC
  324.           ON    Fields.name = CC.CheckConstraintField
  325.         ---------------------------------------------
  326.         ORDER BY  Fields.colid
  327.         ---------------------------------------------
  328.         --SELECT * FROM @MetadataOfTableFields
  329.         ---------------------------------------------
  330.  
  331.  
  332.  
  333.  
  334.  
  335.         UPDATE  @MetadataOfTableFields
  336.         SET     --FieldIsRelativelyUnique = 1,
  337.                 UniquenessBasisField = P.PropertyValue
  338.         FROM    @MetadataOfTableFields F
  339.         JOIN    @ColumnExtendedProperties P
  340.           ON    F.FieldName = P.FieldName
  341.          AND    P.PropertyName =  'UniquenessBasisField'
  342.  
  343.  
  344.  
  345.         UPDATE  @MetadataOfTableFields
  346.         SET     OrdinalBasisField = P.PropertyValue
  347.         FROM    @MetadataOfTableFields F
  348.         JOIN    @ColumnExtendedProperties P
  349.           ON    F.FieldName = P.FieldName
  350.          AND    P.PropertyName =  'OrdinalBasisField'
  351.  
  352.  
  353.  
  354.  
  355.     RETURN
  356. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement