首页 > 解决方案 > SQL Server for loop assistance

问题描述

Ok so this is my stored proc so far.

    ALTER Procedure GetJobInfo()
    AS
    BEGIN
        Select EmployeeId, FirstName, LastName
        From dbo.Employees
        for EmployeeId 
            SELECT ComputerCodeId 
            From dbo.EmployeeJobs 
            Where ComputerCodeId = "F"
    END

I need to get the employees which I have done, but then I need to get the employees jobs from the dbo.EmployeeJobs table that match an array of computer codes. How would I go about doing something like that? My end goal is to be able to create a report that breaks the employee jobs into two categories based on the computer code associated with them and then getting the sum of the two categories and having them populate two different columns.

标签: sqlsql-server

解决方案


我不完全确定你想要什么,但你有这两个选择。

如果您的表 EmployeeJobs 包含 EmployeeId 列,请尝试使用此选项。

SELECT EJ.ComputerCodeId , E.EmployeeId, E.FirstName, E.LastName FROM dbo.EmployeeJobs As EJ INNER JOIN dbo.Employees As E ON EJ.EmployeeId = E.EmployeeId WHERE EJ.ComputerCodeId = 'F'

否则,如果您无法匹配 EmployeeId,您可以使用以下内容,这将按 ComputerCodeId 过滤并由每个员工交叉。

SELECT EJ.ComputerCodeId , E.EmployeeId, E.FirstName, E.LastName FROM dbo.EmployeeJobs As EJ, dbo.Employees As E WHERE EJ.ComputerCodeId = 'F'


推荐阅读