另一个复杂的 SQL 2000 查询问题
我想在查询中使用两个表,tblEmployee
和 tblEmpPerformance
要求是:
使用 tblEmployee.EmpID、tblEmployee.ManagerID
,浏览tblEmpPerformance
并查看tblEmpPerformance.SalaryRaise
和tblEmpPerformance.CommissionRaise
,并相应地更新tblEmployee
。
对于 tblEmpPerformance
中的每条记录,只有这两个字段之一具有非零值。
if tblEmployee.EmpID = tblEmpPerformance.EmpID then
If tblEmpPerformance.Salary > 0 then
update tblEmployee
Set CompensationType = 'Salary'
Where tblEmployee.EmpID = tblEmpPerformance.EmpID
AND tblEmployee.ManagerID = tblEmpPerformance.ManagerID
update tblEmployee
Set SalaryRaise = tblEmpPerformance.SalaryRaise
Where tblEmployee.EmpID = tblEmpPerformance.EmpID
AND tblEmployee.ManagerID = tblEmpPerformance.ManagerID
If tblEmpPerformance.Commission > 0 then
update tblEmployee
Set CompensationType = 'Commission'
Where tblEmployee.EmpID = tblEmpPerformance.EmpID
AND tblEmployee.ManagerID = tblEmpPerformance.ManagerID
update tblEmployee
Set CommissionRaise = tblEmpPerformance.CommissionRaise
Where tblEmployee.EmpID = tblEmpPerformance.EmpID
AND tblEmployee.ManagerID = tblEmpPerformance.ManagerID
如果未找到匹配的 EmpID
,则在 tblContractorPerformance
中查找>
if tblEmployee.EmpID = tblContractorPerformance.EmpID then
If tblContractorPerformance.Salary > 0 then
update tblEmployee
Set CompensationType = 'Salary'
Where tblEmployee.EmpID = tblContractorPerformance.EmpID
AND tblEmployee.ManagerID = tblContractorPerformance.ManagerID
....
....
....
....
Next tblEmployee.EmpID
更新: 哦!!.. 编写此 SQL 查询的最佳方式是什么?存储过程还是游标? 任何有关如何设计查询的建议也会有很大帮助
I have two tables that I want to use in my query, tblEmployee
and tblEmpPerformance
The requirement is:
Using tblEmployee.EmpID, tblEmployee.ManagerID
, go thru tblEmpPerformance
and look at tblEmpPerformance.SalaryRaise
and tblEmpPerformance.CommissionRaise
, and update tblEmployee
accordingly.
Only one of these two fields will have a non-zero value for every record in tblEmpPerformance
if tblEmployee.EmpID = tblEmpPerformance.EmpID then
If tblEmpPerformance.Salary > 0 then
update tblEmployee
Set CompensationType = 'Salary'
Where tblEmployee.EmpID = tblEmpPerformance.EmpID
AND tblEmployee.ManagerID = tblEmpPerformance.ManagerID
update tblEmployee
Set SalaryRaise = tblEmpPerformance.SalaryRaise
Where tblEmployee.EmpID = tblEmpPerformance.EmpID
AND tblEmployee.ManagerID = tblEmpPerformance.ManagerID
If tblEmpPerformance.Commission > 0 then
update tblEmployee
Set CompensationType = 'Commission'
Where tblEmployee.EmpID = tblEmpPerformance.EmpID
AND tblEmployee.ManagerID = tblEmpPerformance.ManagerID
update tblEmployee
Set CommissionRaise = tblEmpPerformance.CommissionRaise
Where tblEmployee.EmpID = tblEmpPerformance.EmpID
AND tblEmployee.ManagerID = tblEmpPerformance.ManagerID
If no matching EmpID
's found, then look in tblContractorPerformance
if tblEmployee.EmpID = tblContractorPerformance.EmpID then
If tblContractorPerformance.Salary > 0 then
update tblEmployee
Set CompensationType = 'Salary'
Where tblEmployee.EmpID = tblContractorPerformance.EmpID
AND tblEmployee.ManagerID = tblContractorPerformance.ManagerID
....
....
....
....
Next tblEmployee.EmpID
Update:
DOH!!..
What would be the best way to write this SQL query. Stored Proc or cursor?
Any suggesstion on how to design the query would be a great help as well
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,您可以将其封装到一个查询中:
As far as I can tell, you can encapsulate this into a single query:
请避免使用光标。考虑基于集合来处理数据。因此,执行您的第一组数据,其中 tblEmpPerformance.Salary > 0 。进行更新,然后对下一组数据执行第二个操作,其中
tblEmpPerformance.Commission > 0 。最后,更新 tblContractorPerformance.Salary > 的所有数据。 0 。
将这些操作放入存储过程中,我相信您会看到比使用游标的等效操作更好的性能。
Please avoid cursors. Think set-based in dealing with your data. So execute on your first set of data, where
tblEmpPerformance.Salary > 0
. Do your updates, then do the second operation on the next set of data wheretblEmpPerformance.Commission > 0
. Lastly, update all the data where thetblContractorPerformance.Salary > 0
.Put these operations in a stored procedure and I'm confident you'll see better performance than the equivalent using cursors.