Skip to content

Instantly share code, notes, and snippets.

@atuttle
Last active January 18, 2016 20:48
Show Gist options
  • Save atuttle/08a912390d43db532b3c to your computer and use it in GitHub Desktop.
Save atuttle/08a912390d43db532b3c to your computer and use it in GitHub Desktop.
Which way is better? Why?
var messageStatus = new util.iQuery("
select
status
from
Message
where
messageId = :id
",{
id: { cfsqltype: "cf_sql_numeric", value: batch.messageId[1] }
});
if (messageStatus.status != "archived"){
new util.iQuery("
update
Message
set
dateTimeArchived = current_timestamp
,status = 'archived'
where
messageId = :id
",{
id: { cfsqltype: "cf_sql_numeric", value: batch.messageId[1] }
});
}
new util.iQuery("
update
Message
set
dateTimeArchived = current_timestamp
,status = 'archived'
where
messageId = :id
and status <> 'archived'
",{
id: { cfsqltype: "cf_sql_numeric", value: batch.messageId[1] }
});
@barneyb
Copy link

barneyb commented Jan 18, 2016

In general, I'd say "b", because it only requires a single round trip to the database (and no resultset inflation, which may or may not matter). Approach "a" also requires a transaction in order to behave correctly, though hopefully you're using one regardless.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment