diff --git a/lib/modules/Sql.pmod/Sql.pike b/lib/modules/Sql.pmod/Sql.pike
index 27cfe2660c453ce09fa02a40160a9947aed0e3d5..f28ff795fe4199eba4539c22cd660c4859e64a5f 100644
--- a/lib/modules/Sql.pmod/Sql.pike
+++ b/lib/modules/Sql.pmod/Sql.pike
@@ -533,6 +533,12 @@ void select_db(string db)
   master_sql->select_db(db);
 }
 
+//! Get the currently selected db if possible.
+string query_db()
+{
+  return master_sql->query_db && master_sql->query_db();
+}
+
 //! Compiles the query (if possible). Otherwise returns it as is.
 //! The resulting object can be used multiple times to the query
 //! functions.