title: Saving dbplyr query (tbl_sql object) to MySQL without saving data locally
body: <p>This question expands on <a href="https://stackoverflow.com/questions/72666187/save-a-tbl-sql-in-r-to-mysql">this question</a></p>
  <p>Here, I'm using the custom function created by @Simon.S.A. shown in the answer to <a href="https://stackoverflow.com/questions/67575456/save-dplyr-query-to-different-schema-in-dbplyr/67577301#67577301">this question</a>. I'm attempting to save a tbl_sql object in R to MySQL as a new table without first saving it locally. Here, the database and schema in my MySQL are named &quot;test.&quot; The tbl_sql object in R is my_data, and I want to save this is a new table in MySQL labeled &quot;car_data&quot;.</p>
  <pre><code>library(DBI)
  library(tidyverse)
  library(dbplyr)
  
  
  #establish connection and import data from MySQL
  
  con &lt;- DBI::dbConnect(RMariaDB::MariaDB(),
                        dbname = &quot;test&quot;,
                        host = &quot;127.0.0.1&quot;,
                        user = &quot;user&quot;,
                        password = &quot;password&quot;)
  
  my_data &lt;- tbl(con, &quot;mtcars&quot;)
  my_data &lt;- my_data %&gt;% filter(mpg &gt;= 22)
  
  # write function to save tbl_sql as a new table in SQL
  
    write_to_database &lt;- function(input_tbl, db, schema, tbl_name){
      # connection
      tbl_connection &lt;- input_tbl$src$con
      
      # SQL query
      sql_query &lt;- glue::glue(
        &quot;SELECT *\n&quot;,
        &quot;INTO {db}.{schema}.{tbl_name}\n&quot;,
        &quot;FROM (\n&quot;,
        dbplyr::sql_render(input_tbl),
        &quot;\n) AS sub_query&quot;
      )
      
      result &lt;- dbExecute(tbl_connection, as.character(sql_query))
    }
  
  
  # execute function
  
  write_to_database(my_data, &quot;test&quot;, &quot;test&quot;, &quot;car_data&quot;)
  </code></pre>
  <p>After running final line, I get the following error. I'm not sure how I can fix this.</p>
  <pre><code>Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.test.car_data
  FROM (
  SELECT *
  FROM `mtcars`
  WHERE (`mpg` &gt;= 22.0)
  ) AS sub_quer' at line 2 [1064]
  12.
  stop(structure(list(message = &quot;You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.test.car_data\nFROM (\nSELECT *\nFROM `mtcars`\nWHERE (`mpg` &gt;= 22.0)\n) AS sub_quer' at line 2 [1064]&quot;, 
  call = NULL, cppstack = NULL), class = c(&quot;Rcpp::exception&quot;, 
  &quot;C++Error&quot;, &quot;error&quot;, &quot;condition&quot;)))
  11.
  result_create(conn@ptr, statement, is_statement)
  10.
  initialize(value, ...)
  9.
  initialize(value, ...)
  8.
  new(&quot;MariaDBResult&quot;, sql = statement, ptr = result_create(conn@ptr, 
  statement, is_statement), bigint = conn@bigint, conn = conn)
  7.
  dbSend(conn, statement, params, is_statement = TRUE)
  6.
  .local(conn, statement, ...)
  5.
  dbSendStatement(conn, statement, ...)
  4.
  dbSendStatement(conn, statement, ...)
  3.
  dbExecute(tbl_connection, as.character(sql_query))
  2.
  dbExecute(tbl_connection, as.character(sql_query))
  1.
  write_to_database(my_data, &quot;test&quot;, &quot;test&quot;, &quot;car_data&quot;)
  
  </code></pre>