title: Create tables in SQL Server using SQL code from RStudio
body: <p>I have SQL code like below, but with more columns and I also create a nonclustered index on this table within the sql script.</p>
  <pre><code>IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES 
                 WHERE TABLE_NAME = ModelOutput)
  BEGIN
      CREATE TABLE ModelOutput
      (
          [OutputID] [bigint] IDENTITY(1,1) NOT NULL,
          [Prediction] [int] NOT NULL,
          [CreateDate] [datetime] DEFAULT CONVERT(DATETIME2(0), getdate()),
      ) ON [PRIMARY]
  END;
  </code></pre>
  <p>This code is saved in a file called <code>CreateTable.sql</code> which I want to run using RStudio.</p>
  <p>Currently, my R code looks like:</p>
  <pre><code>#Read SQL file
  sqlcode = read_file(file = &quot;CreateTable.sql&quot;)
  
  #Run SQL code
  DBI::dbSendQuery(conn = con, statement = sqlcode)
  </code></pre>
  <p>However, when I tried to figure out why it didn't seem to be running, I found that <code>dbSendQuery()</code> only works with select statements (<a href="https://cran.r-project.org/web/packages/DBI/vignettes/DBI-advanced.html" rel="nofollow noreferrer">https://cran.r-project.org/web/packages/DBI/vignettes/DBI-advanced.html</a>). So then I found <code>dbCreateTable()</code> but that only works if I have created the table using R. I don't want to do that since I can't set the same column defaults or create an index--as far as I know.</p>
  <p>I have looked around and cannot find any way to run a create table SQL statement from RStudio. Are there any options for me or do I need to rethink my process entirely to just use <code>dbCreateTable()</code>?</p>