我觉得就两条。 1。防止死锁。 2。注意多用户操作同表或同表用记录时,注意锁表或锁记录。 1 如何锁一个表的某一行A 连接中执行SET TRANSACTION ISOLATION LEVEL REPEATABLE READbegin transelect * from tablename with (rowlock) where id=3waitfor delay '00:00:05'commit tranB连接中如果执行update tablename set colname='10' where id=3 --则要等待5秒update tablename set colname='10' where id<>3 --可立即执行2 锁定数据库的一个表SELECT * FROM table WITH (HOLDLOCK) 注意: 锁定数据库的一个表的区别SELECT * FROM table WITH (HOLDLOCK) 其他事务可以读取表,但不能更新删除SELECT * FROM table WITH (TABLOCKX) 其他事务不能读取表,更新和删除
锁定记录,只允许单用户修改的例子create table #锁表(编号 int)--代码: if exists(select 1 from 编号='你的编号') returninsert #锁表 values('你的编号').....你处理的代码delete #锁表 where 编号='你的编号' -------------------------------------- --为了防止死锁,建议加时间:create table #锁表(编号 int,时间 datetime)--代码: if exists(select 1 from 编号='你的编号' and datediff(ss,时间,getdate())<5 --如果锁的时候超过5秒,则是处理超时 ) returndelete from #锁表 insert #锁表 values('你的编号',getdate()).....你处理的代码delete #锁表 where 编号='你的编号' --------------------------------------- --锁定记录,只允许单用户修改的例子:--创建测试环境 --创建测试表--部门表 create table 部门(departmentid int,name varchar(10))--记录锁定表 create table lock(departmentid int,dt datetime)go --因为函数中不可以用getdate,所以用个视图,得到当前时间 create view v_getdate as select dt=getdate() go --创建自定义函数,判断记录是否锁定 create function f_chk(@departmentid int) returns bit as begin declare @re bit,@dt datetime select @dt=dt from v_getdate if exists(select 1 from lock where departmentid=@departmentid and datediff(ss,dt,@dt)<5) set @re=1 else set @re=0 return(@re) end go--数据处理测试 if dbo.f_chk(3)=1 print '记录被锁定' else begin begin tran insert into lock values(3,getdate()) update 部门 set name='A' where departmentid=3 delete from lock where departmentid=3 commit tran end--删除测试环境 drop table 部门 drop view v_getdate drop function f_chk
1。防止死锁。
2。注意多用户操作同表或同表用记录时,注意锁表或锁记录。
1 如何锁一个表的某一行A 连接中执行SET TRANSACTION ISOLATION LEVEL REPEATABLE READbegin transelect * from tablename with (rowlock) where id=3waitfor delay '00:00:05'commit tranB连接中如果执行update tablename set colname='10' where id=3 --则要等待5秒update tablename set colname='10' where id<>3 --可立即执行2 锁定数据库的一个表SELECT * FROM table WITH (HOLDLOCK)
注意: 锁定数据库的一个表的区别SELECT * FROM table WITH (HOLDLOCK)
其他事务可以读取表,但不能更新删除SELECT * FROM table WITH (TABLOCKX)
其他事务不能读取表,更新和删除
if exists(select 1 from 编号='你的编号')
returninsert #锁表 values('你的编号').....你处理的代码delete #锁表 where 编号='你的编号'
--------------------------------------
--为了防止死锁,建议加时间:create table #锁表(编号 int,时间 datetime)--代码:
if exists(select 1 from 编号='你的编号' and datediff(ss,时间,getdate())<5 --如果锁的时候超过5秒,则是处理超时
)
returndelete from #锁表
insert #锁表 values('你的编号',getdate()).....你处理的代码delete #锁表 where 编号='你的编号'
---------------------------------------
--锁定记录,只允许单用户修改的例子:--创建测试环境
--创建测试表--部门表
create table 部门(departmentid int,name varchar(10))--记录锁定表
create table lock(departmentid int,dt datetime)go
--因为函数中不可以用getdate,所以用个视图,得到当前时间
create view v_getdate as select dt=getdate()
go
--创建自定义函数,判断记录是否锁定
create function f_chk(@departmentid int)
returns bit
as
begin
declare @re bit,@dt datetime
select @dt=dt from v_getdate
if exists(select 1 from lock where departmentid=@departmentid
and datediff(ss,dt,@dt)<5)
set @re=1
else
set @re=0
return(@re)
end
go--数据处理测试
if dbo.f_chk(3)=1
print '记录被锁定'
else
begin
begin tran
insert into lock values(3,getdate())
update 部门 set name='A' where departmentid=3
delete from lock where departmentid=3
commit tran
end--删除测试环境
drop table 部门
drop view v_getdate
drop function f_chk
我sql比较菜!!
create table #锁表(编号 int) 这个不是创建表的语法吗??
"#锁表" 是什么意思啊???
“create table #锁表” 这个是锁表的sql语句吗???
执行这条语句,该表就被锁了??不能修改了??
SQL2000有4个隔离级别,
SQL2008有6个隔离级别.
而典型的并发访问-更新冲突问题应该是另一个问题(通过在软件层次上或数据库层次上加悲观或乐观锁)!
“事务隔离级别”有多少种,该怎么安排呢???我今天又去面试了,记了一招
begin tran
update T_name set age = 21 with(rowlock) where id = 1
waitfor delay '00:00:30'
commit tran
但是没有用上。
是这本书吗?
MICROSOFT SQL SERVER企业级平台管理实践_徐海蔚
http://topic.csdn.net/u/20101220/14/b18ca50f-708a-4548-937d-3f47b49ffec2.html
我不懂那个。我是做.net开发的。。
应聘asp.net程序员值为“并发”重启服务器有用吗??
同时注册?用户名上建个unique约束什么事都没了。