Method: Sequel::Dataset#update
- Defined in:
- lib/sequel/dataset/actions.rb
#update(values = OPTS, &block) ⇒ Object
Updates values for the dataset. The returned value is the number of rows updated. values
should be a hash where the keys are columns to set and values are the values to which to set the columns.
DB[:table].update(x: nil) # UPDATE table SET x = NULL
# => 10
DB[:table].update(x: Sequel[:x]+1, y: 0) # UPDATE table SET x = (x + 1), y = 0
# => 10
Some databases support using multiple tables in an UPDATE query. This requires multiple FROM tables (JOINs can also be used). As multiple FROM tables use an implicit CROSS JOIN, you should make sure your WHERE condition uses the appropriate filters for the FROM tables:
DB.from(:a, :b).join(:c, :d=>Sequel[:b][:e]).where{{a[:f]=>b[:g], a[:id]=>10}}.
update(:f=>Sequel[:c][:h])
# UPDATE a
# SET f = c.h
# FROM b
# INNER JOIN c ON (c.d = b.e)
# WHERE ((a.f = b.g) AND (a.id = 10))
966 967 968 969 970 971 972 973 |
# File 'lib/sequel/dataset/actions.rb', line 966 def update(values=OPTS, &block) sql = update_sql(values) if uses_returning?(:update) returning_fetch_rows(sql, &block) else execute_dui(sql) end end |