diff --git a/core/lib/Drupal/Core/Entity/entity.api.php b/core/lib/Drupal/Core/Entity/entity.api.php
index 9f6ef8b890..b36e0b6605 100644
--- a/core/lib/Drupal/Core/Entity/entity.api.php
+++ b/core/lib/Drupal/Core/Entity/entity.api.php
@@ -1153,10 +1153,10 @@ function hook_entity_predelete(Drupal\Core\Entity\EntityInterface $entity) {
     ->fetchField();
 
   // Log the count in a table that records this statistic for deleted entities.
-  db_merge('example_deleted_entity_statistics')
+  $query = \Drupal::database()->merge('example_deleted_entity_statistics')
     ->key(array('type' => $type, 'id' => $id))
-    ->fields(array('count' => $count))
-    ->execute();
+    ->fields(array('count' => $count));
+  $query->execute();
 }
 
 /**
@@ -1181,10 +1181,10 @@ function hook_ENTITY_TYPE_predelete(Drupal\Core\Entity\EntityInterface $entity)
     ->fetchField();
 
   // Log the count in a table that records this statistic for deleted entities.
-  db_merge('example_deleted_entity_statistics')
+  $query = \Drupal::database()->merge('example_deleted_entity_statistics')
     ->key(array('type' => $type, 'id' => $id))
-    ->fields(array('count' => $count))
-    ->execute();
+    ->fields(array('count' => $count));
+  $query->execute();
 }
 
 /**
diff --git a/core/modules/history/history.module b/core/modules/history/history.module
index 7792591320..7bb786f0e3 100644
--- a/core/modules/history/history.module
+++ b/core/modules/history/history.module
@@ -107,13 +107,13 @@ function history_write($nid, $account = NULL) {
   }
 
   if ($account->isAuthenticated()) {
-    db_merge('history')
+    $query = \Drupal::database()->merge('history')
       ->keys(array(
         'uid' => $account->id(),
         'nid' => $nid,
       ))
-      ->fields(array('timestamp' => REQUEST_TIME))
-      ->execute();
+      ->fields(array('timestamp' => REQUEST_TIME));
+    $query->execute();
     // Update static cache.
     $history = &drupal_static('history_read_multiple', array());
     $history[$nid] = REQUEST_TIME;
diff --git a/core/modules/locale/locale.module b/core/modules/locale/locale.module
index 76b90dfca5..1fc244f5e1 100644
--- a/core/modules/locale/locale.module
+++ b/core/modules/locale/locale.module
@@ -837,7 +837,7 @@ function locale_translation_get_file_history() {
  *   FALSE on failure. Otherwise SAVED_NEW or SAVED_UPDATED.
  */
 function locale_translation_update_file_history($file) {
-  $status = db_merge('locale_file')
+  $status = \Drupal::database()->merge('locale_file')
     ->key(array(
       'project' => $file->project,
       'langcode' => $file->langcode,
@@ -846,8 +846,8 @@ function locale_translation_update_file_history($file) {
       'version' => $file->version,
       'timestamp' => $file->timestamp,
       'last_checked' => $file->last_checked,
-    ))
-    ->execute();
+    ));
+  $status->execute();
   // The file history has changed, flush the static cache now.
   // @todo Can we make this more fine grained?
   drupal_static_reset('locale_translation_get_file_history');
diff --git a/core/modules/search/search.module b/core/modules/search/search.module
index c36aa443d1..81a4e11242 100644
--- a/core/modules/search/search.module
+++ b/core/modules/search/search.module
@@ -209,10 +209,10 @@ function search_update_totals() {
     $total = db_query("SELECT SUM(score) FROM {search_index} WHERE word = :word", array(':word' => $word), array('target' => 'replica'))->fetchField();
     // Apply Zipf's law to equalize the probability distribution.
     $total = log10(1 + 1 / (max(1, $total)));
-    db_merge('search_total')
+    $query = \Drupal::database()->merge('search_total')
       ->key('word', $word)
-      ->fields(array('count' => $total))
-      ->execute();
+      ->fields(array('count' => $total));
+    $query->execute();
   }
   // Find words that were deleted from search_index, but are still in
   // search_total. We use a LEFT JOIN between the two tables and keep only the
@@ -537,7 +537,7 @@ function search_index($type, $sid, $langcode, $text) {
     // If a word already exists in the database, its score gets increased
     // appropriately. If not, we create a new record with the appropriate
     // starting score.
-    db_merge('search_index')
+    $query = \Drupal::database()->merge('search_index')
       ->keys(array(
         'word' => $word,
         'sid' => $sid,
@@ -545,8 +545,8 @@ function search_index($type, $sid, $langcode, $text) {
         'type' => $type,
       ))
       ->fields(array('score' => $score))
-      ->expression('score', 'score + :score', array(':score' => $score))
-      ->execute();
+      ->expression('score', 'score + :score', array(':score' => $score));
+    $query->execute();
     search_dirty($word);
   }
 }
diff --git a/core/modules/shortcut/src/ShortcutSetStorage.php b/core/modules/shortcut/src/ShortcutSetStorage.php
index d76c0c46c1..c2d7df808a 100644
--- a/core/modules/shortcut/src/ShortcutSetStorage.php
+++ b/core/modules/shortcut/src/ShortcutSetStorage.php
@@ -71,10 +71,10 @@ public function deleteAssignedShortcutSets(ShortcutSetInterface $entity) {
    * {@inheritdoc}
    */
   public function assignUser(ShortcutSetInterface $shortcut_set, $account) {
-    db_merge('shortcut_set_users')
+    $query = \Drupal::database()->merge('shortcut_set_users')
       ->key('uid', $account->id())
-      ->fields(array('set_name' => $shortcut_set->id()))
-      ->execute();
+      ->fields(array('set_name' => $shortcut_set->id()));
+    $query->execute();
     drupal_static_reset('shortcut_current_displayed_set');
   }
 
diff --git a/core/modules/taxonomy/taxonomy.module b/core/modules/taxonomy/taxonomy.module
index 80884329bd..754b44286f 100644
--- a/core/modules/taxonomy/taxonomy.module
+++ b/core/modules/taxonomy/taxonomy.module
@@ -529,10 +529,10 @@ function taxonomy_build_node_index($node) {
     // Insert index entries for all the node's terms.
     if (!empty($tid_all)) {
       foreach ($tid_all as $tid) {
-        db_merge('taxonomy_index')
+        $query = \Drupal::database()->merge('taxonomy_index')
           ->key(array('nid' => $node->id(), 'tid' => $tid, 'status' => $node->isPublished()))
-          ->fields(array('sticky' => $sticky, 'created' => $node->getCreatedTime()))
-          ->execute();
+          ->fields(array('sticky' => $sticky, 'created' => $node->getCreatedTime()));
+        $query->execute();
       }
     }
   }
diff --git a/core/modules/tracker/tracker.module b/core/modules/tracker/tracker.module
index 1b2f648df5..b43149038b 100644
--- a/core/modules/tracker/tracker.module
+++ b/core/modules/tracker/tracker.module
@@ -249,16 +249,16 @@ function _tracker_add($nid, $uid, $changed) {
   $changed = max($node->changed, $changed);
 
   // Update the node-level data.
-  db_merge('tracker_node')
+  $query = \Drupal::database()->merge('tracker_node')
     ->key('nid', $nid)
     ->fields(array(
       'changed' => $changed,
       'published' => $node->status,
-    ))
-    ->execute();
+    ));
+  $query->execute();
 
   // Create or update the user-level data, first for the user posting.
-  db_merge('tracker_user')
+  $query = \Drupal::database()->merge('tracker_user')
     ->keys(array(
       'nid' => $nid,
       'uid' => $uid,
@@ -266,8 +266,8 @@ function _tracker_add($nid, $uid, $changed) {
     ->fields(array(
       'changed' => $changed,
       'published' => $node->status,
-    ))
-    ->execute();
+    ));
+  $query->execute();
   // Update the times for all the other users tracking the post.
   db_update('tracker_user')
     ->condition('nid', $nid)
