How to update a record with drupal_write_record? I do not understand how to update a record with drupal_write_record.

I want to update column "vid," in table "node," with the value $value, where "nid" equals $key.

This is what I have, and it doesn't look right, nor does it work. I can't find any examples.

$table = 'node';
$record = new stdClass();
$record-&gt;vid = $value;
drupal_write_record($table, $record, array('nid' =&gt; $key));


To be clear: I want to insert $value into column "vid" where the value $key exists in column "nid"; this is all happening in table node.
drupal_write_record updates an entire record, not a single column. With drupal_write_record you could do this.

$node = node_load($nid);
$node-&gt;vid = $vid;
drupal_write_record('node', $node, 'nid');


But dealing with nodes, you can use node_save instead, which also handles saving fields and other data. Using node_save is recommended way of saving alterations to a node, since it also deals with revisions issues along with fields, hook notification ect. It's simple to use:

$node = node_load($nid);
$node-&gt;vid = $vid;
node_save($node);


If you want to update a single value in a datebase, you can use SQL (db_query) or the db_update function which is two ways of running SQL on your database. The db_update utilizes the Drupal query builder, and will work on any database backend the Drupal supports (now and in the furture):

db_query("UPDATE {node} SET vid = :vid WHERE nid = :nid;",
  array(':nid' =&gt; $nid, ':vid' =&gt; $vid)
);

$num_updated = db_update('node')
  -&gt;fields(array(
    'vid' =&gt; $vid,
  ))
  -&gt;condition('nid $nid)
  -&gt;execute();


drupal_write_record will itself use db_update, a snippet of the code looks like this:

$query = db_update($table)-&gt;fields($fields);
  foreach ($primary_keys as $key) {
    $query-&gt;condition($key, $object-&gt;$key);
  }


Which basicly creates an update query, updating all of the fields, and using all of the values set in primary key as the condition.