why not just set the sku with tokens.
For Drupal commerce you use: http://drupal.org/project/commerce_autosku
For ubercart: http://drupal.org/project/uc_product_power_tools
Once you set up the token rule then all you need to do is load and save all the nodes.
Not real sure what your code above is doing. I would use the devel execute php page and just run a one time script. Something like:
$sql = 'select nid from {node}'
$results = db_query($sql);
while ($nid = db_result($result)) {
$node = node_load($nid);
node_save($node);
}
Been a while since I've used drupal 6 database api so I probably botched the functions above but it should give you the right idea.