MySql、.NET、存储过程与调用客户端共享当前日期和时间

发布于 2024-09-16 05:28:52 字数 413 浏览 4 评论 0原文

我正在编写一个存储过程来更新表:

UPDATE st SET somedate = NOW();

SP 的客户端必须知道 NOW 函数生成的确切日期和时间。

有两种选择:

1) 客户端将输入参数(称为 _now)传递给 SP,为其提供当前日期和时间

UPDATE st SET somedate = _now;

2) SP 将 NOW 的输出返回给客户端,并将其转换为输出参数

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

您认为最好的是什么选项? 还有其他选择吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

日暮斜阳 2024-09-23 05:28:52
  • varnow = now()
  • 更新 st set somedate = varnow
  • 返回 varnow
  • varnow = now()
  • UPDATE st set somedate = varnow
  • return varnow
眼泪淡了忧伤 2024-09-23 05:28:52

我会做这样的事情:

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;

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;
惯饮孤独 2024-09-23 05:28:52

我怀疑这两种方法都是错误的。

SP 的客户必须知道确切的日期和时间

为什么?我怀疑您确实认为客户端必须能够识别受交易影响的记录 - 但使用时间戳来做到这一点是不准确的。问题不只是持续时间超过 1 秒的事务。同一秒内可能会发生两个这样的操作。

如果您有一组记录需要识别为属于某个组,那么必须在模式中表达它们 - 即使假设您从未对表进行进一步更新,大多数事务的时间戳显然也不可靠。

添加另一列或另一表并生成代理键来描述事务。

C.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文