Skip to content

Instantly share code, notes, and snippets.

@jjesusfilho
Last active December 18, 2023 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jjesusfilho/8045bff8c9524f300d5101cefac55f5c to your computer and use it in GitHub Desktop.
Save jjesusfilho/8045bff8c9524f300d5101cefac55f5c to your computer and use it in GitHub Desktop.
Aplica colação ao SQL server para ignorar acentuação e caixa #'
msql_colacao <- function(conn, schema = NULL, table = NULL, ...){
dots <- rlang::ensyms(...) |>
purrr::map_chr(rlang::as_string)
### Verifica se as colunas existem
q <- glue::glue_sql("select top 0 * from {`schema`}.{`table`}", .con = conn)
cols <- DBI::dbGetQuery(conn,q) |>
names()
inexistentes <- purrr::map_lgl(dots, ~{
!is.element(.x, cols)
}) |>
subset(colunas, subset = _)
## Se existem, roda a colação em cada uma delas. Se não, para tudo e informa as
## colunas que não existem.
if (length(inexistentes) == 0) {
purrr::walk(dots, ~{
q <- glue::glue_sql("alter table {`schema`}.{`table`} alter column {`.x`} varchar(max) collate Latin1_General_CI_AI",
.con = conn)
DBI::dbExecute(conn, q )
})
} else {
stop(glue::glue("A(s) coluna(s) {inexistentes} não existem."))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment