views:

20

answers:

3

I'm writing a stored procedure to update a table:

UPDATE st SET somedate = NOW();

The client of the SP must know the exact date and time generated by the NOW function.

There are two options:

1) the client passes an input parameter (called _now) to the SP giving it the current date and time

UPDATE st SET somedate = _now;

2) the SP returns back the NOW's output to the client into an out parameter

UPDATE st SET somedate = NOW();
SELECT somedate FROM st INTO _now;

What do you think is the best option? Are other options possible?

+1  A: 
  • varnow = now()
  • UPDATE st set somedate = varnow
  • return varnow
Michael Pakhantsov
this is the second option. why is it better than the first?
vulkanino
@vulkanino, because it used now() only in one place.
Michael Pakhantsov
because in your method you do 2 db operations an update and a select while michael only does an update - simples.
f00
but my first option is even simpler. I was asking what was the best method and why. the first option doesn't have to RETURN a value.
vulkanino
A: 

I suspect that both approaches are wrong.

client of the SP must know the exact date and time

Why? I suspect you really men that the client must be able to identify the records affected by a transaction - but using a timestamp to do that will not be accurate. And its not just a transaction spanning more than 1 second that is the problem. Potentially two such operations may occur in the same second.

If you've got a set of records which you need to identify as belonging to some group then that must be expressed in the schema - the timestamp of the most transaction is obviously not reliable even assuming that you never have further updates on the table.

Add another column or another table and generate a surrogate key to describe the transaction.

C.

symcbean
I want to let the client know if it has accurate (updated) data but not looking at all the tables in my model.So I do compare the last_updated field in a simple table with the LastUpdated variabile in my client application, if they differ then I have to reload a lot of tables, but if they don't I'm fine.
vulkanino
Thats only going to work if the check is run between each update operation - can you guarantee that? OTOH if you used an incrementing transaction identifier it would be easy to spot when data has not been synchronised and also which data is missing - this is how TCP (and every other stream related networking protocol works). I don't think you've invented a better solution.
symcbean
A: 

i would do something like this:

drop table if exists users;
create table users
(
user_id int unsigned not null auto_increment primary key,
username varchar(32) unique not null,
created_date datetime not null
)
engine=innodb;


delimiter ;

drop procedure if exists insert_user;

delimiter #

create procedure insert_user
(
in uname varchar(32)
)
proc_main:begin

declare id int unsigned default null;
declare created datetime default null;

set created = now();

insert into users (username, created_date) values (uname, created);

set id = last_insert_id();

-- use id elsewhere maybe...

select id as user_id, created as created_date;

end proc_main #


delimiter ;

call insert_user('f00');
call insert_user('bar');

select * from users;
f00