2015-09-10 61 views
-1

我叫員工在數據庫中的表其中有4列,即如何從一個表格生成數據到另一個表格?

  • 僱員EmployeeName指定報告管理器

而它與新的細節,每次一個新的記錄在aspx頁面添加更新。 實例:

EID  EMPLOYEE_NAME  DESIGNATION  REPORTING_MANAGER 
1  TIM COOK   CEO     Null 
2  MILLER    CTO     TIM COOK 
3  BRAD    COO     TIM COOK 
4  WILLIAM    HRD     TIM COOK 
5  JOHN    TECH MANAGER(DEV)  WILLIAM 
6  WALLACE    TECH MANAGER(TEST) WILLIAM 
7  SUNDAR    SWIFT DEV    JOHN 
8  PETER    SWIFT DEV    JOHN 
9  STEVE    TEST ENGINEER   WALLACE 

我想產生類似下面從上述表(表1)表2的數據,

EID  EMPLOYEE_NAME  REPORTING_MANAGER_ID 
1  TIM COOK   Null 
2  MILLER   1 
3  BRAD    1 
4  WILLIAM   1 
5  JOHN    4 
6  WALLACE   4 
7  SUNDAR   5 
8  PETER    5 
9  STEVE    6 

請幫助我。

回答

0
SELECT T1.EID, 
     T1.EMPLOYEE_NAME, 
     T1.DESIGNATION, 
     T1.REPORTING_MANAGER, 
     T2.EID AS REPORTING_MANAGER_ID 
FROM EMPLOYEE T1 
     LEFT JOIN EMPLOYEE T2 
       ON T1.EMPLOYEE_NAME = T2.REPORTING_MANAGER 
1

簡單:

  1. 創建一個存儲過程以從表1與左數據插入表2 JOIN插入在表2 /更新行中不存在表1中。

  2. 調用後新的細節,這個存儲過程插入(我假設表1中插入或更新是通過存儲過程)

0

您可以通過多種方式做到這一點。

隨着LEFT JOIN

SELECT e.EID , 
     e.EMPLOYEE_NAME , 
     e.DESIGNATION , 
     e.REPORTING_MANAGER, 
     rm.EID AS REPORTING_MANAGER_ID 
FROM Employee e 
LEFT JOIN Employee rm ON rm.EMPLOYEE_NAME = e.REPORTING_MANAGER 

子查詢路線:

SELECT e.EID , 
     e.EMPLOYEE_NAME , 
     e.DESIGNATION , 
     e.REPORTING_MANAGER, 
     (SELECT TOP 1 rm.EID 
     FROM Employee rm 
     WHERE rm.EMPLOYEE_NAME = e.REPORTING_MANAGER) AS REPORTING_MANAGER_ID 
FROM Employee e 
+0

感謝唐納的幫助。 –

相關問題