2014-06-26 75 views
0

我有一條SELECT語句,它根據我發送給它的員工ID的XML字符串查找記錄。TSQL在從select中獲取數據後插入到表中

它檢查某些標準以及檢查數據是否存在於另一個表中。

一旦返回數據,我需要那麼數據添加到它不是在發現表。

,我只是不知道如何,因爲我認爲這個代碼塊每次運行這會工作然後進入下一個插入行。

我怎樣才能將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

插入語句應該插入到'taxTrackerTracked'還是其他表? –

+0

更正到跟蹤表中。示例:獲取不在該表中的數據,現在將其添加到該表中,但我也有XML輸出中的數據與原始選擇一起使用 – SBB

回答

0

我並不是一個真正的XQuery專家,但是你不能只是將你的查詢輸出捕獲到一個XML變量中,然後將該變量返回給客戶端,然後再使用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) 
+0

這似乎工作得很好 - 只需將值類型從int更改爲varchar(我們的id包含兩者) – SBB

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語句表名稱&列名的實際名稱。

+0

..數據仍然會輸出到我需要的XML中?似乎很容易哈哈 - 謝謝! – SBB

+0

這個問題是我不需要我的select語句中的所有數據;只是員工;不知道它是否會以這種方式工作 – SBB

相關問題