Incorrect syntax near ';'. Expecting conversation

Dominic Naimool :

I am attempting to execute an R (specifically dplyr) script in SQL as part of an ETL stored procedure. Both the R code and the SQL Query appear to be successful I believe the problem lies when attempting to manipulate the output The error I was thrown the following error : "Incorrect syntax near ';'. Expecting conversation"

My source for this structure is : https://docs.microsoft.com/en-us/sql/advanced-analytics/r/creating-workflows-that-use-r-in-sql-server?view=sql-server-ver15

EXECUTE sp_execute_external_script @language = N'R'
    , @script = N'
    rm(list = ls(all.names = TRUE))
library(dplyr)
library(tidyr)
library(rlang)
library(lubridate)
        OutputDataSet <- InputDataSet %>% mutate(Date=as.Date(effective)) %>% select(-effective) %>%
  group_by(rowno_dmddmhi_dmd) %>%
  tidyr::complete(Date = seq.Date(min(Date), max(Date), by="day"))%>% fill(balance)%>% 
  mutate(amount = ifelse(is.na(amount), 0, amount))%>%mutate(opp_amount = amount * -1) %>%
  arrange(rowno_dmddmhi_dmd,desc(Date)) %>% group_by(rowno_dmddmhi_dmd) %>%
  mutate(cumsum = cumsum(opp_amount))%>%group_by(rowno_dmddmhi_dmd)%>%
  mutate(balance.at.date=balance + cumsum)%>%ungroup() %>% mutate(Report_Date=as.Date(Date)-1)%>%select(rowno_dmddmhi_dmd,Date,balance.at.date,Report_date);'
    , @input_data_1 = N'SELECT data_.rowno_dmddmhi_dmd,
       dmd.balance,
       Sum(data_.amount) AS amount,
       data_.effective
FROM   (
       --Top False Cap 
       SELECT dmhi.rowno,
              dmhi.rowno_dmddmhi_dmd,
              0                               AS amount,
              CONVERT(Date, Getdate()) AS effective
       FROM   ks208.dbo.dmd
              INNER JOIN dmon208.dbo.dmhi
                      ON dmd.rowno = dmhi.rowno_dmddmhi_dmd
       UNION
       SELECT dmhi.rowno,
              dmhi.rowno_dmddmhi_dmd,
              dmhi.amount,
              CONVERT(Date, dmhi.effective) AS effective
       FROM   ks208.dbo.dmd
              INNER JOIN ks208.dbo.dmhi
                      ON dmd.rowno = dmhi.rowno_dmddmhi_dmd
       UNION
       SELECT dmhi.rowno,
              dmhi.rowno_dmddmhi_dmd,
              dmhi.amount,
              CONVERT(Date, dmhi.effective) AS effective
       FROM   dmon208.dbo.dmd
              INNER JOIN dmon208.dbo.dmhi
                      ON dmd.rowno = dmhi.rowno_dmddmhi_dmd
        -- False Bottom Cap
        UNION
        SELECT dmhi.rowno,
               dmhi.rowno_dmddmhi_dmd,
               0                                                  AS amount,
               CONVERT(Date, Dateadd(year, -4, Getdate())) AS effective
        FROM   ks208.dbo.dmd
               INNER JOIN dmon208.dbo.dmhi
                       ON dmd.rowno = dmhi.rowno_dmddmhi_dmd) AS data_
       INNER JOIN ks208.dbo.dmd
               ON dmd.rowno = data_.rowno_dmddmhi_dmd
WHERE  Year(data_.effective) >= Year(Getdate()) - 4


GROUP  BY data_.rowno_dmddmhi_dmd,
          data_.effective,
          dmd.balance  
'

    with RESULT sets (("rowno_dmddmhi_dmd" int not null, "Date" Date not null, "balance.at.date" float not null, "Report_date" Date not null ));
    end;  


Clifford Piehl :

I notice in your structure source the author is defining a test-table at the top of his query. He uses 'GO' commands to do this and then start his procedure. You do not have a process like this above your stored procedure, so the end statement at the end of your query is redundant. Remove it and see what happens?

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=390817&siteId=1