Search code examples
kdb+

kdb/Q How to update single row in table?


I'm trying to find out how I can update a single value in a single, specified row in my table.

What I need is to go from this:

table - before

Index Value
0 a
1 b
2 c

to this, specifically targeting row with Index at 2:

table - after

Index Value
0 a
1 c
2 c

I think something like this is a start: table: update Value:?[Value="b";"c";Value] from table[rowIndex], but this only updates and returns the one row, I'm trying to find a way to keep the table as is and ONLY alter the value in the given row.


Solution

  • You can use a where clause to only update where the condition is met, as long as there is only one row that meets the condition:

    table: update Value:"c" from table where Value="b"
    

    If there is more than one row meeting the condition you may want to filter using rowIndex:

    table: update Value:?[Value="b";"c";Value] from table where i=rowIndex
    

    OR you might be able to skip the conditional:

    table: update Value:"c" from table where i=rowIndex