| Title: | Tools to Work with Microsoft SQL Server Databases via 'RODBC' |
|---|---|
| Description: | Tools that extend the functionality of the 'RODBC' package to work with Microsoft SQL Server databases. Makes it easier to browse the database and examine individual tables and views. |
| Authors: | Arni Magnusson [aut, cre] |
| Maintainer: | Arni Magnusson <[email protected]> |
| License: | GPL-3 |
| Version: | 1.0.1 |
| Built: | 2026-05-08 05:25:21 UTC |
| Source: | https://github.com/gfcm/mssql |
Tools that extend the functionality of the RODBC package to work with Microsoft SQL Server databases. Makes it easier to browse the database and examine individual tables and views.
Browse database:
dbOverview |
Dimensions and column names |
dbStorage |
Storage size |
dbTime |
Time created and modified |
Browse table:
tableDim |
Dimensions |
tableHead |
First rows |
tableNcol |
Number of columns |
tableNrow |
Number of rows |
tableOverview |
Data types and dimensions |
Helper functions:
tableQuote |
Quote table name |
browseVignettes() shows a vignette with implementation notes.
Arni Magnusson.
This package complements the RODBC package and does not replace the standard query methods.
For example, the user may find dbOverview and
tableOverview more convenient than the underlying
sqlTables and sqlColumns, but to
query the database sqlQuery or sqlFetch are still used in the
normal way.
Get dimensions and first few column names of tables and views in a database.
dbOverview(channel, schema = "dbo", dim = TRUE, peek = 2, ...)dbOverview(channel, schema = "dbo", dim = TRUE, peek = 2, ...)
channel |
an RODBC connection. |
schema |
database schema. |
dim |
whether to calculate the number of rows and columns for each table/view. |
peek |
how many column names to show. The value |
... |
passed to |
The dim = FALSE option results in faster computation, but the
Rows and Cols columns will only contain NA values.
Similarly, the peek = FALSE results in faster computation, but the
First column will only contain NA values. These options can be
useful to get a quick overview of a large database.
Data frame containing six columns:
Name |
name of table/view. |
Schema |
database schema. |
Type |
type of table/view. |
Rows |
number of rows. |
Cols |
number of columns. |
First |
first column names. |
sqlTables is the underlying function used to get the
list of tables/views, tableDim is used to count rows and
columns, and sqlColumns is used to peek at the first
column names.
dbStorage shows the storage size of tables and
dbTime shows the time when tables/views were created and last
modified.
MSSQL-package gives an overview of the package.
## Not run: con <- odbcConnect("myDatabase") dbOverview(con) dbOverview(con, dim=FALSE, peek=FALSE) ## End(Not run)## Not run: con <- odbcConnect("myDatabase") dbOverview(con) dbOverview(con, dim=FALSE, peek=FALSE) ## End(Not run)
Get storage size of tables in a database.
dbStorage(channel, total = TRUE, used = FALSE, unused = FALSE)dbStorage(channel, total = TRUE, used = FALSE, unused = FALSE)
channel |
an RODBC connection. |
total |
whether to calculate total storage size. |
used |
whether to calculate used storage size. |
unused |
whether to calculate unused storage size. |
Data frame containing the following columns:
Name |
name of table/view. |
Schema |
database schema. |
Type |
type of table/view. |
Rows |
number of rows. |
Cols |
number of columns. |
In addition, any of the following columns, depending on which of
total, used, and unused are TRUE:
TotalKB |
total storage size. |
UsedKB |
used storage size. |
UnusedKB |
unused storage size. |
Based on https://stackoverflow.com/questions/7892334.
sqlQuery is the underlying function used to query
sys.tables, sys.indexes, sys.partitions,
sys.allocation_units, and sys.schemas.
dbOverview shows the dimensions of tables/views and the first
column names, and dbTime shows the time when tables/views were
created and last modified.
object.size is the base function to return the storage size of
objects inside the R workspace.
MSSQL-package gives an overview of the package.
## Not run: con <- odbcConnect("myDatabase") dbOverview(con) dbOverview(con, dim=FALSE, peek=FALSE) ## End(Not run)## Not run: con <- odbcConnect("myDatabase") dbOverview(con) dbOverview(con, dim=FALSE, peek=FALSE) ## End(Not run)
Get time information about tables and views: when they were created and when they were last modified.
dbTime(channel)dbTime(channel)
channel |
an RODBC connection. |
Data frame containing five columns:
Name |
name of table/view. |
Schema |
database schema. |
Type |
type of table/view. |
Created |
time created. |
Modified |
time last modified. |
sqlQuery is the underlying function used to query
sys.tables and sys.views.
dbOverview shows the dimensions of tables/views and the first
column names, and dbStorage shows the storage size of tables.
Sys.time is the base function to show the current time.
MSSQL-package gives an overview of the package.
## Not run: con <- odbcConnect("myDatabase") dbTime(con) ## End(Not run)## Not run: con <- odbcConnect("myDatabase") dbTime(con) ## End(Not run)
Return the number of rows and columns in a database table.
tableDim(channel, sqtable)tableDim(channel, sqtable)
channel |
an RODBC connection. |
sqtable |
a database table or view. |
Vector of length two, containing the number of rows and columns.
tableNrow and tableNcol are the underlying
functions to get the number of rows and columns in a database table.
dim is the base function to return the dimensions for data
frames inside the R workspace.
MSSQL-package gives an overview of the package.
## Not run: con <- odbcConnect("myDatabase") tableDim(con, "sysusers") ## End(Not run)## Not run: con <- odbcConnect("myDatabase") tableDim(con, "sysusers") ## End(Not run)
Return the first rows of a database table.
tableHead(channel, sqtable, n = 3)tableHead(channel, sqtable, n = 3)
channel |
an RODBC connection. |
sqtable |
a database table or view. |
n |
number of rows to get. |
Data frame with the first n rows of the database table or view.
This function can be used to examine the structure of a table or view, along with some example data values.
sqlQuery with tableQuote are the
underlying functions used to query the table/view.
head is the base function to return the first parts of an
object inside the R workspace.
tableOverview shows the data types and dimensions of a database
table.
MSSQL-package gives an overview of the package.
## Not run: con <- odbcConnect("myDatabase") tableHead(con, "sysusers") t(tableHead(con, "sysusers", 1)) ## End(Not run)## Not run: con <- odbcConnect("myDatabase") tableHead(con, "sysusers") t(tableHead(con, "sysusers", 1)) ## End(Not run)
Return the number of columns in a database table.
tableNcol(channel, sqtable)tableNcol(channel, sqtable)
channel |
an RODBC connection. |
sqtable |
a database table or view. |
Number of columns as integer.
tableDim and tableNrow also return the dimensions
of a database table.
sqlColumns is the underlying function used to examine
the table columns.
ncol is the base function to return the number of columns for
data frames inside the R workspace.
MSSQL-package gives an overview of the package.
## Not run: con <- odbcConnect("myDatabase") tableNcol(con, "sysusers") ## End(Not run)## Not run: con <- odbcConnect("myDatabase") tableNcol(con, "sysusers") ## End(Not run)
Return the number of rows in a database table.
tableNrow(channel, sqtable)tableNrow(channel, sqtable)
channel |
an RODBC connection. |
sqtable |
a database table or view. |
Number of rows as integer.
tableDim and tableNcol also return the dimensions
of a database table.
sqlQuery is the underlying function used to examine the
table rows.
nrow is the base function to return the number of rows for data
frames inside the R workspace.
MSSQL-package gives an overview of the package.
## Not run: con <- odbcConnect("myDatabase") tableNrow(con, "sysusers") ## End(Not run)## Not run: con <- odbcConnect("myDatabase") tableNrow(con, "sysusers") ## End(Not run)
Show data types and dimensions of a database table.
tableOverview(channel, sqtable, max = 1000)tableOverview(channel, sqtable, max = 1000)
channel |
an RODBC connection. |
sqtable |
a database table or view. |
max |
number of rows to analyze the resulting data frame columns in R.
Pass |
List containing Cols and Rows, describing column data types and
the number of rows.
sqlColumns, sqlQuery, and
tableNrow are the underlying functions used to examine the
table/view.
class is the base function to show the class of an object
inside the R workspace.
tableHead returns the first rows of a database table.
MSSQL-package gives an overview of the package.
## Not run: con <- odbcConnect("myDatabase") tableOverview(con, "sysusers") tableOverview(con, "sysusers")$Cols ## End(Not run)## Not run: con <- odbcConnect("myDatabase") tableOverview(con, "sysusers") tableOverview(con, "sysusers")$Cols ## End(Not run)
Add special quotes around table name.
tableQuote(sqtable)tableQuote(sqtable)
sqtable |
table name, with or without schema name. |
String with special quotes.
The sqlQuery function requires special quotes if the table name has
spaces. Furthermore, the schema name must not be inside the special quotes.
sqlQuery requires special quotes if the table name has
spaces.
Quotes in base R.
MSSQL-package gives an overview of the package.
tableQuote("table") tableQuote("table name") tableQuote("schema.table") tableQuote("schema.table name")tableQuote("table") tableQuote("table name") tableQuote("schema.table") tableQuote("schema.table name")