1

I want to update a column through a specific column variable. For example, I want to create a SQL update statement update t1 set col1 = col1 * 3, where t1 is a table and col1 is a column name. To make the statement work, a column object is needed, so I need to generate a column object by name. Does anyone know how to do this? If so, can you share some best practices?

serendipity
  • 104
  • 5

1 Answers1

1

You can use Metaprogramming. The function sqlCol is a good choice.

For example, for the following update job t.price = t.price * 3,

where t is a table and price is a column, you can use the following 3 ways:

t1=table(`A`A`B`B as symbol, 2021.04.15 2021.04.16 2021.04.15 2021.04.16 as date, 12 13 21 22 as price)
col="price"
t1[col]=t1[col]*3  //Method 1
t1[col]=expr(sqlCol(col),*,3)  //Method 2
sqlUpdate("t1",sqlColAlias(expr(sqlCol(col),*,3),col)).eval()  //Method 3

Note: Method 1 and Method 2 are only applicable to in-memory tables.

zhihengqu
  • 371
  • 5