Teradata R Package Function Reference - 16.20 - td_set_context - Teradata R Package

Teradata® R Package Function Reference

prodname
Teradata R Package
vrm_release
16.20
created_date
February 2020
category
Programming Reference
featnum
B700-4007-098K

Description

A context must be initialized before executing analytic functions. Use the td_set_context function to initialize a context using an already established connection.
Note: If a context already exists, then this removes all nonpersistent work tables but does not disconnect the connection in the existing context, unless it was created using td_create_context .

Usage

td_set_context(con, temp.database = NULL)

Arguments

con

Required Argument
Specifies a DBIConnection object with a Teradata subclass.

temp.database

Optional Argument
Specifies the database name which needs to be used to create temporary database objects (table/view) for processing data. Such objects are garbage collected at the end of the session. If a user wants to specifically use a known database for temporary objects, the database name can be specified using this argument.
Note:

  1. Make sure the user has privilege to create objects in this database, otherwise the functions that create temporary objects will fail.

  2. If this argument is not specified, default database of the connection is used for temporary objects.

  3. To get the temporary database name, use function td_get_context().

Default: NULL
Types: character

Value

Returns NULL invisibly

See Also

td_remove_context, td_create_context, td_get_context

Examples

# Establish an ODBC connection to Teradata Vantage using the dbConnect API.
con <- DBI::dbConnect(odbc::odbc(), dsn = "TeradataDSN")

# Example 1: Changing the temp.database of an existing context without removing  
# nonpersistent work tables.
# Use td_set_context function to initialize a context with a given connection.
td_set_context(con = con, temp.database = "database_for_transient_objects")

# Use td_get_context function to get the current context information.
context <- td_get_context()

# Change the temp.database of an existing context.
td_set_context(context$connection, temp.database = context$default.database)

# Remove the current context.
td_remove_context()

# Example 2: Managing multiple connections with td_set_context.
con1 <- DBI::dbConnect(odbc::odbc(), dsn = "TeradataDSN")
con2 <- DBI::dbConnect(odbc::odbc(), dsn = "TeradataDSN")

# Initialize the context with con1.
td_set_context(con = con1, temp.database = "database_for_transient_objects")

# Initialize the context with con2.
td_set_context(con = con2)

# Remove the current context.
# Note: This performs garbage collection and disconnects current context connection i.e., con2.
td_remove_context()

# Explicit call to disconnect the connection con1.
DBI::dbDisconnect(con1)

# Example 3: Create a new ODBC context and switch its temporary database using 
# td_set_context.
con <- td_create_context(dsn = "TeradataDSN")

# Use td_get_context function to get the current context information.
td_get_context()

# Use td_set_context function to change temporary database.
td_set_context(con, temp.database = "database_for_transient_objects")

# Use td_get_context function to get the current context information.
td_get_context()

# Remove the current context.
td_remove_context()

# Example 4: Use dbConnect to connect to Advanced SQL Engine through Teradata SQL Driver  
# and set context using different temporary database.
db <- DBI::dbConnect(tdplyr::NativeDriver(), <host_name>, <username>, <password>)

# Use td_set_context function to set context using different temporary database.
td_set_context(db, temp.database = "database_for_transient_objects")

# Remove the current context.
td_remove_context()