Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 10.23 KB | None | 0 0
  1. USE [SDS_DevSchoolDistrict]
  2. GO
  3. /****** Object:  UserDefinedFunction [dbo].[MetadataOfProcedureParameters]    Script Date: 05/16/2011 21:23:33 ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8.  
  9. --SELECT * FROM MetadataOfProcedureParameters('AddAssessment')
  10. -- =================================================================================
  11. -- Copyright:   School Data Solutions, LLC
  12. -- =================================================================================
  13. -- =================================================================================
  14.  
  15. -- Sample Execution
  16. /*
  17.  
  18.  
  19. SELECT * FROM MetadataOfTableFields('Schools')
  20.     SELECT
  21.               *
  22.     FROM      dbo.MetadataOfProcedureParameters('AddStudentAssessmentTestScore')
  23.  
  24. */
  25. -- =================================================================================
  26. ALTER FUNCTION [dbo].[MetadataOfProcedureParameters]
  27. (
  28.       @ProcedureName                    sysname
  29. )
  30. --DECLARE @ProcedureName sysname        SET @ProcedureName = 'AddAssessment'
  31. --DECLARE @OutputTable  TABLE
  32. RETURNS @OutputTable    TABLE
  33. (
  34.       ParameterName                     sysname
  35.     , ParameterOrdinal                  int
  36.     , ParameterDataType                 sysname
  37.     , ParameterValueType                sysname
  38.     , ParameterInterfaceType            sysname
  39.  
  40.     , ParameterMaximumLength            int DEFAULT('')
  41.     , ParameterIsNumeric                bit
  42.     , ParameterIsRequired               bit
  43.     , ParameterDefaultValue             varchar(50) DEFAULT('')
  44.     , ParameterRequiresSelection        bit  DEFAULT('')
  45.     , ParameterConstraint               varchar(500)  DEFAULT('')
  46.     , ParameterIsOutput                 bit
  47.  
  48.     , ParameterSelectionSource          sysname
  49.     --, CorrespondingTableName            sysname
  50.     --, CorrespondingFieldName            sysname
  51. )
  52. AS
  53. BEGIN
  54.     -------   ======================================================================
  55.  
  56.  
  57.     -------   ======================================================================
  58.     -------   Declarations
  59.     -------   ======================================================================
  60.     DECLARE   @PrimaryKeyFields          TABLE
  61.     (
  62.               TableName                 sysname
  63.             , PrimaryKeyFieldName       sysname
  64.           --  , RepresentativeFieldName sysname
  65.     )
  66.     -------   ======================================================================
  67.  
  68.  
  69.  
  70.  
  71.     -------   ======================================================================
  72.     -------   Declarations
  73.     -------   ======================================================================
  74.     DECLARE   @ProcedureParameterDefaultValues          TABLE
  75.     (
  76.               ParameterName                 sysname
  77.             , ParameterType                 varchar(50)
  78.             , ParameterDefaultValue         varchar(100)
  79.             , ParameterIsOutput             bit
  80.             , ParameterLine                 varchar(1000)
  81.     )
  82.  
  83.     -------   ----------------------------------------------------------------------
  84.     DECLARE   @ProcedureText        nvarchar(max)
  85.     SET       @ProcedureText    =   ''
  86.  
  87.  
  88.     -------   ======================================================================
  89.     -------   Parse Procedure Parameter List
  90.     -------   ======================================================================
  91.  
  92.     -------   Retrieve Procedure Definition
  93.     SELECT
  94.               @ProcedureText =  ProcedureDefinitions.definition                
  95.     FROM      sys.procedures    Procedures
  96.     JOIN      sys.sql_modules   ProcedureDefinitions
  97.               on  ProcedureDefinitions.object_id    = Procedures.object_id
  98.     WHERE     Procedures.name       = @ProcedureName
  99.  
  100.  
  101.     -------   Collapse White Space to Single Spaces Only
  102.     SET       @ProcedureText = dbo.RegExReplace(@ProcedureText, '\s+' , ' ')
  103.  
  104.  
  105.     -------   Trap Procedure Parameters, discarding unnecessary text
  106.     SET       @ProcedureText = dbo.RegExReplace(@ProcedureText, '(^.*CREATE\s+PROC.*\[' + @ProcedureName + '\]\s+)|(AS\s+BEGIN.*$)', '')
  107.  
  108.  
  109.     -------    Remove in-line comments in parameters
  110.     SET       @ProcedureText = dbo.RegExReplace(@ProcedureText, '--(\s*[^,])*(,){0,1}' , '$2')
  111.  
  112.     -------    Remove comments blocks in parameters
  113.     SET       @ProcedureText = dbo.RegExReplace(@ProcedureText, '/\*.*\*/' , '')
  114.  
  115.  
  116.     -------   ======================================================================
  117.     -------   Populate Table Variable With Parameter Default Values
  118.     -------   ======================================================================
  119.     INSERT    @ProcedureParameterDefaultValues
  120.     SELECT
  121.               dbo.RegExReplace([Value], '(\S*)\s.*', '$1')                              AS  ParameterName
  122.             , dbo.RegExReplace([Value], '(\S*)\s*(\S*).*', '$2')                        AS  ParameterDataType
  123.             , CASE  dbo.RegExReplace([Value], '(.*)=\s*(.*)(OUTPUT|OUT)*', '$2')
  124.                     WHEN    [Value]
  125.                         THEN    NULL
  126.                     ELSE        dbo.RegExReplace([Value], '(.*)=\s*(.*)(OUTPUT|OUT)*', '$2')
  127.               END                                                                       AS  ParameterDefaultValue
  128.             , CASE
  129.                     WHEN    dbo.RegExReplace([Value], '.*(OUTPUT|OUT)\s*', '$1') IN ('OUTPUT', 'OUT')
  130.                         THEN    1
  131.                     ELSE        0
  132.               END                                                                       AS  ParameterIsOutput
  133.             , [Value]                                                                   As  ParameterLine
  134.     FROM      dbo.SplitList(@ProcedureText, ',')
  135.     -------   -----------------------------------------------------------------------
  136.     -- SELECT 'defaults', * FROM @ProcedureParameterDefaultValues
  137.     -------   ======================================================================
  138.  
  139.  
  140.  
  141.    
  142.  
  143.  
  144.  
  145.     -------   ======================================================================
  146.     -------   Populate Return Table
  147.     -------   ======================================================================
  148.     INSERT    @OutputTable (
  149.                   ParameterName                    
  150.                 , ParameterOrdinal                  
  151.                 , ParameterDataType                
  152.                 , ParameterValueType                
  153.                 , ParameterInterfaceType            
  154.  
  155.                 , ParameterMaximumLength            
  156.                 , ParameterIsNumeric                
  157.                 , ParameterIsRequired              
  158.                 , ParameterDefaultValue            
  159.                 , ParameterRequiresSelection        
  160.                 , ParameterConstraint              
  161.                 , ParameterIsOutput                
  162.  
  163.                 , ParameterSelectionSource
  164.             --  , CorrespondingTableName            
  165.             --  , CorrespondingFieldName            
  166.             )
  167.     -------   ----------------------------------------------------------------------
  168.     SELECT    REPLACE(PARAMETER_NAME, '@', '')                  AS  ParameterName
  169.             , ORDINAL_POSITION                                  AS  ParameterOrdinal
  170.             , ISNULL(DATA_TYPE, '')                             AS  ParameterDataType
  171.             , ISNULL(USER_DEFINED_TYPE_NAME, '')                AS  ParameterValueType
  172.             , dbo.InterfaceType (     DATA_TYPE
  173.                                     , USER_DEFINED_TYPE_NAME
  174.                                     , CASE WHEN(D.ParameterDefaultValue  IS NULL) THEN 0 ELSE 1 END
  175.                                     , CHARACTER_MAXIMUM_LENGTH
  176.                                 )                                           AS  ParameterInterfaceType
  177.             , CHARACTER_MAXIMUM_LENGTH                                      AS  ParameterMaximumLength
  178.             , dbo.DatatypeIsNumeric(DATA_TYPE)                              AS  ParameterIsNumeric
  179.             , CASE WHEN(D.ParameterDefaultValue IS NULL)THEN 1 ELSE 0 END   AS  ParameterIsRequired
  180.             , D.ParameterDefaultValue                                       AS  ParameterDefaultValue
  181.             , NULL                                                          AS  ParameterRequiresSelection
  182.             , NULL                                                          AS  ParameterConstraint
  183.             , CASE WHEN(PARAMETER_MODE = 'IN') THEN 0 ELSE 1 END            AS  ParameterIsOutput
  184.             , ISNULL(NULL, '')                                              AS  SelectionSource
  185.             --, ISNULL(NULL, '')                                                AS  CorrespondingTableName
  186.             --, ISNULL(NULL, '')                                                AS  CorrespondingFieldName
  187.     FROM    INFORMATION_SCHEMA.PARAMETERS
  188.             --------------------------------------------------------------------------
  189.     JOIN    @ProcedureParameterDefaultValues D -- DefaultValues
  190.       ON    D.ParameterName = PARAMETER_NAME
  191.             -------------------------------------------------------------------------- 
  192.     WHERE   SPECIFIC_NAME = @ProcedureName
  193.     --AND   PARAMETER_MODE = 'IN'
  194.     ORDER
  195.        BY   ORDINAL_POSITION
  196.     -------   --------------------------------------------------------------------------
  197.  
  198.  
  199.  
  200.     ----------------------------------------------------------------------
  201.     -- If there are any parameters of a "Reference" type
  202.     -- we need to retrieve the table and fields neccessary for selecting a value
  203.     ----------------------------------------------------------------------
  204.     IF(EXISTS(SELECT 1 FROM @OutputTable WHERE ParameterValueType LIKE '%Reference%'))
  205.     BEGIN
  206.             -------   ======================================================================
  207.             -------   First we try to match parameter names against existing Primary-Key fields
  208.             -------   ======================================================================
  209.             INSERT @PrimaryKeyFields          
  210.             (       TableName,
  211.                     PrimaryKeyFieldName
  212.             )
  213.             SELECT  pk.table_name,
  214.                     column_name as 'primary_key'
  215.             FROM    information_schema.table_constraints pk
  216.             JOIN    information_schema.key_column_usage c
  217.               ON    c.table_name = pk.table_name
  218.              AND    c.constraint_name = pk.constraint_name
  219.             JOIN    (  
  220.                         SELECT  pk.table_name,
  221.                                 COUNT(DISTINCT column_name) PrimaryKeyFieldCount
  222.                         FROM    information_schema.table_constraints pk
  223.                         JOIN    information_schema.key_column_usage c
  224.                           ON    c.table_name = pk.table_name
  225.                          AND    c.constraint_name = pk.constraint_name
  226.                         WHERE   constraint_type = 'primary key'
  227.                         GROUP
  228.                            BY   pk.table_name
  229.                         HAVING  COUNT(DISTINCT column_name) = 1
  230.                     ) E -- ElementTables (single primary key field             
  231.               ON    c.table_name = E.table_name
  232.             WHERE   constraint_type = 'primary key'
  233.             -------   ======================================================================
  234.                
  235.            
  236.            
  237.             UPDATE  @OutputTable
  238.             SET     ParameterSelectionSource = PK.TableName,
  239.                     ParameterRequiresSelection = 1
  240.             --      CorrespondingTableName = PK.TableName,
  241.             --      CorrespondingFieldName = PK.PrimaryKeyFieldName
  242.             --SELECT    PK.*, P.*
  243.             FROM    @OutputTable P
  244.             JOIN    @PrimaryKeyFields PK   
  245.               ON    P.ParameterName = PK.PrimaryKeyFieldName
  246.              AND    P.ParameterValueType LIKE '%Reference%'        
  247.            
  248.     END
  249.     ----------------------------------------------------------------------
  250.     -- SELECT * FROM @OutputTable
  251.     -------   ======================================================================
  252.     RETURN
  253. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement