1
t:([] col1:`aa`bb`cc;col2:`aaa`bbb`ccc);  
field1:`col1;  
field2:`col2;  
v1:`aa;  
v2:`aaaa; 

I want same result by functional update as

update col2:`aaaa from t where col1=`aa;  

I tried

![t;enlist (=;field1;enlist v1);0b;(enlist field2)!(enlist v2)]; 

but it give me an error 'aaaa

appreciate if some one can help.

Thomas Smyth - Treliant
  • 4,993
  • 6
  • 25
  • 36
sxzhangzsx
  • 65
  • 9

3 Answers3

4
![t;enlist (=;field1;enlist v1);0b;(enlist field2)!(enlist enlist v2)]
emc211
  • 1,369
  • 7
  • 14
  • If you want coverage of all functional forms: http://www.timestored.com/kdb-guides/functional-queries-dynamic-sql#functional-update – Ryan Hamilton Jul 25 '17 at 07:09
2

You can use the parse function to convert the update expression to a parse tree from which you can easily read off the proper arguments for the functional update.

q)parse"update col2:`aaaa from t where col1=`aa"
!
`t
,,(=;`col1;,`aa)
0b
(,`col2)!,,`aaaa

Recall that unary , is enlist, so the functional form of your query would be

![t;enlist(=;`col1;enlist `aa);0b;(enlist `col2)!enlist enlist `aaaa]

or, using variable names,

![t;enlist(=;field1;enlist v1);0b;(enlist field2)!(enlist enlist v2)]
Alexander Belopolsky
  • 2,228
  • 10
  • 26
0

It's not in functional update form, but here's another way you can updated your table

 @[t; where t[`col1] =`aa; :; enlist[`col1]!enlist[`aaaa]]
Conor Cadogan
  • 210
  • 1
  • 4