Skip to content
Advertisement

How to specify vartype for sqlSave() for multiple columns without manually typing in R?

Some reproducible code. Note that your database and server name may be different.

This is how i export a dataframe to sql management studio as a table and it works but, say I have one hundred new columns in iris. Do I have to specify each column name = to decimal(28,0) in my columnTypes variable?

By default the columns go in as floats as least in my actual dataframe (iris is just the example), so that’s why I need to manually change them in columnTypes. I want everything after the 5 original columns in iris to be decimal(28,0) format without manually including them in columnTypes.

Advertisement

Answer

I did not read into the sqlSave() statement and possible alternatives. Meaning: there might be a more apropriate solution. Anyhow you can generate the list of wanted definitions in base R by repitition and combining lists:

Since you example code seems to be working for you, this should also though (judging by the output) – I did not test it with a DB, as I have no test setup available atm.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement