Learn R Programming

lazysql (version 0.1.3)

natural_key: Create SQL string for joining on matching natural keys

Description

Create string with SQL expressions for WHERE clause to join two tables on the given columns.

Usage

natural_key(table_names, key_columns)

Arguments

table_names
[character(2)] Name of data base tables to be joined.
key_columns
[character(1:Inf)] Names of key columns in both tables.

Value

Character string to be used in SQL statement.

Details

The names of tables and key columns must be valid SQL identifiers. They are validated to conform to the regular expression returned by valid_identifier_regex.

The SQL string is created in 3 steps:

  1. Combine table names with key names, eg, "PRL.FLIGHT_NR".
  2. Create logical expressions, eg, "PRL.FLIGHT_NR = PRL_SSR.FLIGHT_NR"
  3. Concatenate logical expressions by "and" to form final SQL esxpression.

See Also

valid_identifier_regex.

Examples

Run this code
# SQL expression
(sql_expr <- lazysql::natural_key(c("TAB1", "tab_2"),c("COL1", "col_2")))

# sample SQL JOIN statement
paste("select * from TAB1, TAB2 where", sql_expr)

Run the code above in your browser using DataLab