DECLARE @TableID NVARCHAR(20);
DECLARE @AliasId NVARCHAR(18);
DECLARE @EditSize SMALLINT;
DECLARE @RealSize SMALLINT;
DECLARE @statement NVARCHAR(MAX);
DECLARE @params NVARCHAR(MAX);
DECLARE alfa_udfs CURSOR FOR
  SELECT
    TableID
    ,AliasID
    ,EditSize
  FROM
    cufd
  WHERE  TypeID = 'A';
OPEN alfa_udfs;
FETCH NEXT FROM alfa_udfs INTO @TableID, @AliasId, @EditSize;
WHILE @@FETCH_STATUS = 0
  BEGIN
      IF EXISTS (SELECT
                   *
                 FROM
                   sys.tables
                 WHERE  name = @TableId)
        BEGIN
            SET @RealSize = 0
            SET @params = '@RealSizeOut smallint output';
            SET @statement = 'select @RealSizeOut = max(len(U_' + @AliasID + ')) from [' + @TableId + '] where U_' + @AliasID + ' is not null';
            EXECUTE SP_EXECUTESQL
              @statement,
              @params,
              @RealSizeOut = @RealSize OUTPUT;
            IF @RealSize IS NOT NULL
              BEGIN
                  IF @EditSize < @RealSize
                    BEGIN
                        SELECT
                          @TableID         AS TableName
                          ,'U_' + @AliasId AS UDFName
                          ,@RealSize       AS RealSize
                          ,@EditSize       AS DefinedSize
                    END
              END
        END
      FETCH NEXT FROM alfa_udfs INTO @TableID, @AliasId, @EditSize;
  END
CLOSE alfa_udfs;
DEALLOCATE alfa_udfs;

