另一个复杂的 SQL 2000 查询问题

发布于 2024-10-20 01:58:31 字数 1826 浏览 2 评论 0原文

我想在查询中使用两个表,tblEmployeetblEmpPerformance

要求是:

使用 tblEmployee.EmpID、tblEmployee.ManagerID,浏览tblEmpPerformance并查看tblEmpPerformance.SalaryRaisetblEmpPerformance.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 技术交流群。

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

发布评论

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

评论(2

墨落画卷 2024-10-27 01:58:31

据我所知,您可以将其封装到一个查询中:

Update tblEmployee
Set CompensationType = Case
                        When EP.Commission > 0 Then 'Commission'
                        When EP.Salary > 0 Then 'Salary'
                        When CP.Salary > 0 Then 'Salary'
                        Else E.CompensationType -- leave value as is
                        End
    , CommissionRaise = Case
                        When EP.Commission > 0 Then EP.CommissionRaise
                        Else E.CommissionRaise -- leave value as is
                        End
    , SalaryRaise = Case
                        When EP.Commission > 0 Then E.SalaryRaise
                        When EP.Salary > 0 Then EP.SalaryRaise
                        When CP.Salary > 0 Then CP.SalaryRaise
                        Else E.SalaryRaise -- leave value as is
                        End
From tblEmployee As E
    Left Join tblEmployeePerformance As EP
        On EP.EmpID = E.EmpID
            And EP.ManagerId = E.ManagerId
    Left Join tblContractorPerformance As CP
        On CP.EmpID = E.EmpID
            And CP.ManagerId = E.ManagerId

As far as I can tell, you can encapsulate this into a single query:

Update tblEmployee
Set CompensationType = Case
                        When EP.Commission > 0 Then 'Commission'
                        When EP.Salary > 0 Then 'Salary'
                        When CP.Salary > 0 Then 'Salary'
                        Else E.CompensationType -- leave value as is
                        End
    , CommissionRaise = Case
                        When EP.Commission > 0 Then EP.CommissionRaise
                        Else E.CommissionRaise -- leave value as is
                        End
    , SalaryRaise = Case
                        When EP.Commission > 0 Then E.SalaryRaise
                        When EP.Salary > 0 Then EP.SalaryRaise
                        When CP.Salary > 0 Then CP.SalaryRaise
                        Else E.SalaryRaise -- leave value as is
                        End
From tblEmployee As E
    Left Join tblEmployeePerformance As EP
        On EP.EmpID = E.EmpID
            And EP.ManagerId = E.ManagerId
    Left Join tblContractorPerformance As CP
        On CP.EmpID = E.EmpID
            And CP.ManagerId = E.ManagerId
罗罗贝儿 2024-10-27 01:58:31

请避免使用光标。考虑基于集合来处理数据。因此,执行您的第一组数据,其中 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 where tblEmpPerformance.Commission > 0. Lastly, update all the data where the tblContractorPerformance.Salary > 0.

Put these operations in a stored procedure and I'm confident you'll see better performance than the equivalent using cursors.

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