Turn on comments for all posts
using wordPress query()
$sql=" UPDATE $wpdb->posts SET comment_status='open' "; $wpdb->query($sql);
Using normal sql
UPDATE wp_posts SET comment_status='open';
Turn off comments for all posts
using wordpress query()
$sql=" UPDATE $wpdb->posts SET comment_status='close' "; $wpdb->query($sql);
using normal sql
UPDATE wp_posts SET comment_status='close';
ref:
https://codex.wordpress.org/Class_Reference/wpdb