Last active
December 18, 2023 20:28
-
-
Save jjesusfilho/8045bff8c9524f300d5101cefac55f5c to your computer and use it in GitHub Desktop.
Aplica colação ao SQL server para ignorar acentuação e caixa #'
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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