我有一个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
答案 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)