tsql_validate_field_types | R Documentation |
tsql_validate_field_types
checks whether a named vector of TSQL data
types is compatible with a given data.table that you wish to upload to
Microsoft SQL Server. The function does not cover every possible situation!
For example, you might want to push your R 'POSIXct
' column to a SQL Server
table as an 'nvarchar()
' datatype, but this function will expect you to map
it to a more typical data type such as 'datetime
'. Think of this function
as a second set of eyes to make sure you didn't do something careless.
tsql_validate_field_types(ph.data = NULL, field_types = NULL)
ph.data |
The name of a single data.table/data.frame to be loaded to SQL Server. |
field_types |
A named character vector with the desired TSQL datatypes
for your upload. For example, |
Note that this function may not thoroughly evaluate if the allocated
length for character strings, i.e., nvarchar()
and varchar()
, is
sufficient.
Note that this replaces rads::validate_yaml_data
. To use this function to
check the compatibility of field types in a yaml file with your dataset, do
the following:
load the yaml file, e.g., yaml <- yaml::read_yaml("X:/code/myyaml.yaml")
unlist the variable descriptions, e.g., yaml_field_types = unlist(yaml$vars)
use tsql_validate_field_types
, e.g., tsql_validate_field_types(ph.data = mydt, field_types = yaml_field_types)
# example of a success
library(data.table)
mydt = data.table(col1 = 1:10000L, # creates integers
col2 = 1:10000/3) # creates floats
mydt[, col3 := as.Date(Sys.Date()) - col1] # creates dates
mydt[, col4 := as.character(col3)] # create strings
mydt[, col5 := Sys.time()] # create POSIXct
myfieldtypes <- c(col1 = 'int',
col2 = 'float',
col3 = 'date',
col4 = 'nvarchar(255)',
col5 = 'datetime')
tsql_validate_field_types(ph.data = mydt, field_types = myfieldtypes)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.