Say I have two tables, Employee and PayRate. Employee has a primary key EmpID which is a foreign key to PayRate. I need to update any matched employees whose pay rate is greater than 1. I have to consider both performance and mutual lock. Here are two solutions: 1. SELECT then UPDATE select @cnt=count(*) from employee where [email protected] and [email protected] and empid in (select empid from payrate where rate >1) if @cnt > 1 begin update employee set status = 1 where [email protected] and [email protected] and empid in (
if @cnt > 1 begin update employee set status = 1 where [email protected] and [email protected] and empid in (select empid from payrate where rate > 1) 2. UPDATE directly update employee set status = 1 where [email protected] and [email protected] and empid in (select empid from payrate where rate > 1)
【在 f*****e 的大作中提到】 : Say I have two tables, Employee and PayRate. Employee has a primary key : EmpID which is a foreign key to PayRate. I need to update any matched : employees whose pay rate is greater than 1. I have to consider both : performance and mutual lock. Here are two solutions: : 1. SELECT then UPDATE : select @cnt=count(*) from employee where [email protected] and [email protected] and empid : in (select empid from payrate where rate >1) : if @cnt > 1 : begin : update employee set status = 1 where [email protected] and [email protected] and empid in
k*0
5 楼
哦,这么便宜,哪里有?我再找找。 可以开贴讨论下最便宜好用的缝纫机了。
B*g
6 楼
乱,看不懂。不看了,凑活用merge
in
【在 f*****e 的大作中提到】 : Say I have two tables, Employee and PayRate. Employee has a primary key : EmpID which is a foreign key to PayRate. I need to update any matched : employees whose pay rate is greater than 1. I have to consider both : performance and mutual lock. Here are two solutions: : 1. SELECT then UPDATE : select @cnt=count(*) from employee where [email protected] and [email protected] and empid : in (select empid from payrate where rate >1) : if @cnt > 1 : begin : update employee set status = 1 where [email protected] and [email protected] and empid in
【在 f*****e 的大作中提到】 : Say I have two tables, Employee and PayRate. Employee has a primary key : EmpID which is a foreign key to PayRate. I need to update any matched : employees whose pay rate is greater than 1. I have to consider both : performance and mutual lock. Here are two solutions: : 1. SELECT then UPDATE : select @cnt=count(*) from employee where [email protected] and [email protected] and empid : in (select empid from payrate where rate >1) : if @cnt > 1 : begin : update employee set status = 1 where [email protected] and [email protected] and empid in
M*N
9 楼
cute 好用吗?
z*y
10 楼
The first solution actually duplicated in select statement. To minimize the lock time or avoiding lock escalation, make sure you have non clustered index on rate column in payrate table. What would happen if you don't have non clustered index on rate column is that query optimizer would upgrade to table level share lock if it think it was more efficient than row level share lock. When table level share lock was applied to table, it would exclude any update or insert operations onto this table.
【在 f*****e 的大作中提到】 : Say I have two tables, Employee and PayRate. Employee has a primary key : EmpID which is a foreign key to PayRate. I need to update any matched : employees whose pay rate is greater than 1. I have to consider both : performance and mutual lock. Here are two solutions: : 1. SELECT then UPDATE : select @cnt=count(*) from employee where [email protected] and [email protected] and empid : in (select empid from payrate where rate >1) : if @cnt > 1 : begin : update employee set status = 1 where [email protected] and [email protected] and empid in
s*e
11 楼
I did.Can only sew very small item, Better to have a bigger one
z*y
12 楼
Actually in this senario, there are no difference. A friend Andy Lenard wrote a CLR function to tell how it works internally, he found that sql server will return true as soon as it found first matched record. In this senario, it would not fetch data before sending true or false. Another way to verify this is from the execution plan.
【在 j*****n 的大作中提到】 : use IF EXISTS instead of count(*) : : in