TSQL从select中获取后插入表中

时间:2014-06-26 22:40:34

标签: sql sql-server tsql stored-procedures

我有一个select语句,它根据我发送给它的员工ID的XML字符串查找记录。

它检查某些标准以及检查数据是否存在于另一个表中。

一旦它返回数据,我需要将该数据添加到未找到的表中。

我只是不确定这是如何工作的,因为我认为这个代码块每次都会在到达下一个插入行之前运行。

如何将每个A.[employee]插入到它返回的另一个表中?

SELECT   A.[employee],
                 Sup.SupEmpID,
                 Sup.Email,
                 Sup.FirstName,
                 Sup.LastName,
                 sum(A.[awardValue]) AS totalAwarded
        FROM     taxTracker AS A
                 INNER JOIN
                 EmpTable AS E
                 ON A.employee = E.EmpID
                 INNER JOIN
                 EmpTable AS Sup
                 ON E.SupEmpID = Sup.EmpID
        WHERE    YEAR(A.[awardDate]) = YEAR(GETDATE())
                 AND employee IN (SELECT ParamValues.x1.value('empID[1]', 'VARCHAR(50)')
                                  FROM   @awardEmployees.nodes('/employees/employee') AS ParamValues(x1))
                 AND NOT EXISTS (SELECT employee
                                 FROM   taxTrackerTracked
                                 WHERE  [year] = YEAR(GETDATE())
                                        AND employee IN (SELECT ParamValues.x1.value('empID[1]', 'VARCHAR(50)')
                                                         FROM   @awardEmployees.nodes('/employees/employee') AS ParamValues(x1)))
        GROUP BY employee, Sup.SupEmpID, Sup.Email, Sup.Firstname, Sup.LastName
        HAVING   SUM(A.[awardValue]) > '75.00'
        FOR      XML PATH ('data'), TYPE, ELEMENTS, ROOT ('root');

--An insert statement here

2 个答案:

答案 0 :(得分:0)

只需在SELECT之前添加INSERT语句。

INSERT INTO OtherTable (employee, SupEmpID, Email, FirstName, LastName, totalAwarded)
    SELECT   A.[employee],
                     Sup.SupEmpID,
                     Sup.Email,
                     Sup.FirstName,
                     Sup.LastName,
                     sum(A.[awardValue]) AS totalAwarded
            FROM     taxTracker AS A
                     INNER JOIN
                     EmpTable AS E
                     ON A.employee = E.EmpID
                     INNER JOIN
                     EmpTable AS Sup
                     ON E.SupEmpID = Sup.EmpID
            WHERE    YEAR(A.[awardDate]) = YEAR(GETDATE())
                     AND employee IN (SELECT ParamValues.x1.value('empID[1]', 'VARCHAR(50)')
                                      FROM   @awardEmployees.nodes('/employees/employee') AS ParamValues(x1))
                     AND NOT EXISTS (SELECT employee
                                     FROM   taxTrackerTracked
                                     WHERE  [year] = YEAR(GETDATE())
                                            AND employee IN (SELECT ParamValues.x1.value('empID[1]', 'VARCHAR(50)')
                                                             FROM   @awardEmployees.nodes('/employees/employee') AS ParamValues(x1)))
            GROUP BY employee, Sup.SupEmpID, Sup.Email, Sup.Firstname, Sup.LastName
            HAVING   SUM(A.[awardValue]) > '75.00'
            FOR      XML PATH ('data'), TYPE, ELEMENTS, ROOT ('root');

修复表名称& insert语句中的列名称为您的实际名称。

答案 1 :(得分:0)

我不是真正的XQuery专家,但你不能只是将查询的输出捕获到XML变量中,然后SELECT将变量返回给客户端,然后再次使用XQuery(你“已经在查询中使用它”来提取您需要的值并将其与INSERT语句一起使用。像这样:

DECLARE @x XML = (
    SELECT
        A.[employee],
        Sup.SupEmpID, Sup.Email, Sup.FirstName, Sup.LastName,
        sum(A.[awardValue]) AS [totalAwarded]
    FROM taxTracker AS A
    INNER JOIN EmpTable AS E
        ON A.employee = E.EmpID
    INNER JOIN EmpTable AS Sup
        ON E.SupEmpID = Sup.EmpID
    WHERE YEAR(A.[awardDate]) = YEAR(GETDATE())
    AND employee IN (
        SELECT ParamValues.x1.value('empID[1]', 'VARCHAR(50)')
        FROM @awardEmployees.nodes('/employees/employee') AS ParamValues(x1)
    )
    AND NOT EXISTS (
        SELECT employee
        FROM   taxTrackerTracked
        WHERE  [year] = YEAR(GETDATE())
        AND employee IN (
            SELECT ParamValues.x1.value('empID[1]', 'VARCHAR(50)')
            FROM @awardEmployees.nodes('/employees/employee') AS ParamValues(x1)
        )
    )
    GROUP BY employee, Sup.SupEmpID, Sup.Email, Sup.Firstname, Sup.LastName
    HAVING SUM(A.[awardValue]) > '75.00'
    FOR XML PATH ('data'), TYPE, ELEMENTS, ROOT ('root')
)

SELECT @x

INSERT taxTrackerTracked ([employee])
SELECT r.d.query('data(employee)').value('/', 'INT') AS [employee]
FROM @x.nodes('/root/data') r (d)