关于并发修改的头痛问题

zheng 2003-01-20 11:32:31
两个用户同时打开delphi开发的应用程序修改一个表(用的是TQuery控件,不是Table,缓存更新方式),在第一个用户修改提交后,怎么保证第二个用户提交时发现这条记录已被其它用户修改(包括不同的字段).
(MS SQL SERVER 修改的触发器是after触发,不像Oracle一样有Before?如何是好?)
...全文
82 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
tjan 2003-01-22
  • 打赏
  • 举报
回复
第二个修改的用户应该有 2 个缓冲区:inserted 和 deleted,其中 deleted 缓冲区的内容与第一个用户修改前的内容是一致的,这样,你就可以用这个内容与记录的当前内容去比较,应该可以识别出来了。
Sunny3141 2003-01-22
  • 打赏
  • 举报
回复
timestamp
chenchangfu 2003-01-22
  • 打赏
  • 举报
回复
没有遇到过,关注中
zheng 2003-01-22
  • 打赏
  • 举报
回复
我觉得都不解决问题啊,大家试过没有?
remanwang 2003-01-21
  • 打赏
  • 举报
回复
应该不用管,这类问题sql server能自己处理
happydreamer 2003-01-21
  • 打赏
  • 举报
回复
可以用自动变量@@DBTS来访问数据库产生的最后一个时戳

create table #test(c1 int identity,c2 int default,c3 timestamp)
insert #test default values
insert #test default values
insert #test default values


select * from #test

1 0 0x00000000000000CB
2 0 0x00000000000000CC
3 0 0x00000000000000CD
4 0 0x00000000000000CE
5 0 0x00000000000000CF
6 0 0x00000000000000D0

select @@dbts
0x00000000000000D0


update #test set c2=c1

select * from #test
1 1 0x00000000000000D1
2 2 0x00000000000000D2
3 3 0x00000000000000D3
4 4 0x00000000000000D4
5 5 0x00000000000000D5
6 6 0x00000000000000D6
zheng 2003-01-20
  • 打赏
  • 举报
回复
timestamp我已经试过,但由于sqlserver是after update 触发的,触发器中读取时timestamp已经被修改,inserted 表中的timestamp这时也已经是前一个修改生成的,无法得到本人打开界面时的timestamp.
pengdali 2003-01-20
  • 打赏
  • 举报
回复
对用timestamp字段可以搞定!
DainelLee 2003-01-20
  • 打赏
  • 举报
回复
在前台程序中处理
TQuery控件提供这样的功能,你看一下帮助,有个属性是存放修改前的数据,
CABO 2003-01-20
  • 打赏
  • 举报
回复
timestamp
timestamp 这种数据类型表现自动生成的二进制数,确保这些数在数据库中是唯一的。timestamp 一般用作给表行加版本戳的机制。存储大小为 8 字节。

注释
Transact-SQL timestamp 数据类型与在 SQL-92 标准中定义的 timestamp 数据类型不同。SQL-92 timestamp 数据类型等价于 Transact-SQL datetime 数据类型。

Microsoft® SQL Server™ 将来的版本可能会修改 Transact-SQL timestamp 数据类型的行为,使它与在标准中定义的行为一致。到那时,当前的 timestamp 数据类型将用 rowversion 数据类型替换。

Microsoft® SQL Server™ 2000 引入了 timestamp 数据类型的 rowversion 同义词。在 DDL 语句中尽可能使用 rowversion 而不使用 timestamp。rowversion 受数据类型同义词行为的制约。有关更多信息,请参见数据类型同义词。

在 CREATE TABLE 或 ALTER TABLE 语句中,不必为 timestamp 数据类型提供列名:

CREATE TABLE ExampleTable (PriKey int PRIMARY KEY, timestamp)

如果没有提供列名,SQL Server 将生成 timestamp 的列名。rowversion 数据类型同义词不具有这样的行为。指定 rowversion 时必须提供列名。

一个表只能有一个 timestamp 列。每次插入或更新包含 timestamp 列的行时,timestamp 列中的值均会更新。这一属性使 timestamp 列不适合作为键使用,尤其是不能作为主键使用。对行的任何更新都会更改 timestamp 值,从而更改键值。如果该列属于主键,那么旧的键值将无效,进而引用该旧值的外键也将不再有效。如果该表在动态游标中引用,则所有更新均会更改游标中行的位置。如果该列属于索引键,则对数据行的所有更新还将导致索引更新。

不可为空的 timestamp 列在语义上等价于 binary(8) 列。可为空的 timestamp 列在语义上等价于 varbinary(8) 列。
CABO 2003-01-20
  • 打赏
  • 举报
回复
加入一个时间戳字段(可做版本号),它是自动更新的,每次更新前与自己的版本对比
leimin 2003-01-20
  • 打赏
  • 举报
回复
if you use SQL SERVER 2K,you can use instead of trigger.

SQL Server 2000 allows you create a second kind of trigger, called an instead-of trigger. An instead-of trigger, rather than the data modification operation that fires the triggers, specifies the action to take. Instead-of triggers are different from after triggers in several ways:

You can have only one instead-of trigger for each action (INSERT, UPDATE, and DELETE).
You cannot combine instead-of triggers and foreign keys that have been defined with CASCADE on a table. For example, if Table2 has a FOREIGN KEY constraint that references Table1 and specifies CASCADE as the response to DELETE operations, you will get an error message if you try to create an instead-of trigger for DELETE on Table2. However, you can have instead-of triggers for INSERT or UPDATE. Similarly, if you already have an instead-of trigger on Table2, you cannot alter the table to add a foreign key constraint with the CASCADE action for the same data modification operation.
Instead-of triggers can never be recursive, regardless of the setting of the recursive triggers database option. For example, if an instead-of trigger is executed for INSERT into Table1 and the trigger does an INSERT into Table1, the instead-of trigger is not processed. Instead, the INSERT is processed as if there were no instead-of trigger for INSERT, and any constraints and after triggers will take effect.
weixxxp 2003-01-20
  • 打赏
  • 举报
回复
用instead of触发器
饮水需思源 2003-01-20
  • 打赏
  • 举报
回复
用事务控制
yelook 2003-01-20
  • 打赏
  • 举报
回复
把CursorLocation改为clUseServer就行了
tjan 2003-01-20
  • 打赏
  • 举报
回复
SQL Server 有 before 触发器啊
当我们在执行一些DML,DDL,甚至desc tablename等等操作的时候,会话就hang住了, 还有一种情况,当我们使用create or replace procedure/function等语句修改Procedure和Function的时候,会话也会hang住,这是为什么呢? 当出现上述情况的时候,我们可以通过v$session_wait查询等待事件,当然,这种情况下,该session的等待事件一定是'Library cache lock'。 让我们来解释一下, 第一种情况,当会话1(session 1)在对一个表执行DML 或者 DDL,与此同时还有另一个会话,我们姑且称之为会话2(session 2),这个会话2也在对这个表执行DDL(如ALTER TABLE),当会话2的完成需要很长时间时(依操作的具体的数据量而定),会话1就会hang住,这时,你查询会话1的等待事件就是'Library cache lock'。 第二种情况,当会话1(session 1)在修改一个package,与此同时还有另一个会话,我们姑且称之为会话2(session 2),这个会话2正在执行会话1所修改的package中的Procedure或者Function,会话1就会hang住,这时,你查询会话1的等待事件就是'Library cache lock'。 因此,在对Package/Procedure/Function/View进行编译和分析的时候,我们必须确定此时没有人正在编译和分析相同的对象,即确保没有人也在此时改变这些需要重定义(drop和recreate)的对象的定义。

22,300

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧