Wordpress和mysql程序

I'm trying to alt WP tables with the function. Mysql code works fine if I call it from mysql client (adds columns), but when I called it via $wpdb->query() it gave me error pointing to DELIMITER line. Upon some googling, I found out that I should use mysqli for this purpose. Now the error log is empty but columns are still not created. Anyone got an idea of what I'm doing wrong? Function code is below

global $wpdb;
$tables=array(
    'wp_users',
    'wp_usermeta',
    'wp_term_taxonomy',
    'wp_term_relationships',
    'wp_terms',
    'wp_taxonomymeta',
    'wp_posts',
    'wp_postmeta',
    'wp_p2p',
    'wp_p2pmeta',
    'wp_options',
    'wp_fb_user_timezone',
    'wp_fb_coaching_call_report_team_user',
    'wp_fb_coaching_call_report_team',
    'wp_fb_3d_notes',
    'wp_comments',
    'wp_commentmeta',
    'wp_coaching_teams_student',
    'wp_coaching_teams_coach',
    'wp_coaching_teams',
    'wp_coaching_call_count'
);
if (is_array($tables)&&count($tables)>0)
{
    foreach ($tables as $table)
    {
        $aaa=mysqli_multi_query($wpdb->dbh,'DELIMITER $$
                        DROP PROCEDURE IF EXISTS add_createdat_and_updatedat_fields $$
                        CREATE PROCEDURE add_createdat_and_updatedat_fields()
                        BEGIN
                            IF EXISTS(SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_schema=DATABASE() AND table_name=\''.$table.'\') THEN
                                IF NOT EXISTS ((SELECT * FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name=\''.$table.'\' AND column_name=\'created_at\')) THEN
                                ALTER TABLE `'.$table.'` ADD `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP;
                                END IF;  
                                IF NOT EXISTS ((SELECT * FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name=\''.$table.'\' AND column_name=\'updated_at\')) THEN
                                ALTER TABLE `'.$table.'` ADD `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP;
                                END IF;
                            END IF;
                        END $$
                        CALL add_createdat_and_updatedat_fields() $$
                        DROP PROCEDURE IF EXISTS add_createdat_and_updatedat_fields $$ 
                    DELIMITER;');
    }
}

Do not use multiquery, run each command separately with mysqli_query() instead, that way you don't need manipulating separators at all.