2013-06-04 220 views
2

enter image description here 我在我的數據庫中有這種結構。我需要建立一個員工姓名清單,然後是他們的部門和辦公室名稱。現在我來此查詢:查詢中的SQL查詢

ALTER PROCEDURE FilterEmpList 
    @empName nvarchar(250) = null, 
    @empDepID int = null, 
    @empOfficeID int = null, 
    @empPosID int = null  
AS 
BEGIN 
SELECT 
    E.emp_id AS 'emp_id', 
    E.emp_name AS 'emp_name',  
    (SELECT pos_name FROM Positions WHERE pos_id=EP.pos_id) AS 'pos_name', 
    (SELECT dep_name FROM Departments WHERE dep_id=OS.dep_id) AS 'dep_name', 
    (SELECT office_name FROM Offices WHERE office_id=OS.office_id) AS 'office_name' 
FROM 
    Org_Structure OS INNER JOIN (Emp_Positions EP INNER JOIN Employees E ON EP.emp_id=E.emp_id) ON OS.chain_id=EP.chain_id  
WHERE 
    (E.emp_name LIKE '%'[email protected]+'%' OR @empName IS NULL) 
    AND 
    (OS.dep_id = @empDepID OR @empDepID IS NULL) 
    AND 
    (OS.office_id = @empOfficeID OR @empOfficeID IS NULL) 
    AND 
    (EP.pos_id = @empPosID OR @empPosID IS NULL) 
END 

當我執行這種方式EXEC FilterEmpList,出現錯誤:無效的對象名稱EP「

這對我來說非常困難,這個查詢是我的最大功課,並且讓我度過了一整天......請幫助我提供建議。

下一個問題

現在,我已經有了這個查詢多了一個問題。如果Org_Structure中的office_id或dep_id爲NULL,則根本不會返回該記錄。 WHERE子句有問題。

這個問題與的解決方案,其中從句位於此我的另一主題:WHERE clause - record with a NULL column is not displayed

謝謝大家。該問題已解決

回答

1

這可能是有益的你 -

ALTER PROCEDURE dbo.FilterEmpList 
    @empName nvarchar(250) = null, 
    @empDepID int = null, 
    @empOfficeID int = null, 
    @empPosID int = null  
AS 
BEGIN 

    SELECT 
     E.emp_id, 
     E.emp_name,  
     p.pos_name, 
     d.dep_name, 
     o.office_name 
    FROM dbo.Org_Structure OS 
    JOIN dbo.Emp_Positions EP ON OS.chain_id=EP.chain_id 
    /*LEFT*/ JOIN dbo.Employees E ON EP.emp_id=E.emp_id 
    /*LEFT*/ JOIN dbo.Positions p ON p.pos_id=EP.pos_id 
    /*LEFT*/ JOIN dbo.Departments d ON d.dep_id=OS.dep_id 
    JOIN dbo.Offices o ON o.office_id=OS.office_id 
    WHERE (
       E.emp_name LIKE '%'[email protected]+'%' 
      OR 
       @empName IS NULL 
     ) 
     AND OS.dep_id = ISNULL(@empDepID, OS.dep_id) 
     AND OS.office_id = ISNULL(@empOfficeID, OS.office_id) 
     AND EP.pos_id = ISNULL(@empPosID, EP.pos_id) 

END 
+0

謝謝,我會在一小時內嘗試併發送反饋。 – SWA

+0

好吧,我剛剛嘗試過你編輯過的例子,它似乎工作),我會在幾分鐘內用真實數據嘗試它。 – SWA

+0

** WHERE **有問題。如果** OS **中的某個字段爲NULL(例如,對於某些員工** office_id = NULL **,因爲它們屬於整個部門),則該員工不會出現在搜索結果中... – SWA

1

你們當中有些人的代碼也放錯了地方,我認爲。試試這個...

SELECT 
    E.emp_id AS 'emp_id', 
    E.emp_name AS 'emp_name',  
    P.pos_name AS 'pos_name', 
    D.dep_name AS 'dep_name', 
    O.office_name AS 'office_name' 
FROM 
    Org_Structure OS 
    INNER JOIN Emp_Positions EP ON OS.chain_id=EP.chain_id 
    INNER JOIN Employees E ON EP.emp_id=E.emp_id  
    LEFT JOIN Positions P ON P.pos_id=EP.pos_id 
    LEFT JOIN Departments D ON D.dep_id=OS.dep_id 
    LEFT JOIN Offices O ON O.office_id=OS.office_id 
WHERE 
    (E.emp_name LIKE '%'[email protected]+'%' OR @empName IS NULL) 
    AND 
    (OS.dep_id = @empDepID OR @empDepID IS NULL) 
    AND 
    (COALESCE(O.office_id,@empOfficeID) = @empOfficeID OR @empOfficeID IS NULL) 
    AND 
    (EP.pos_id = @empPosID OR @empPosID IS NULL) 
+0

我發現你的例子中唯一的區別 - 在FROM部分。我修改了我的查詢,但仍顯示相同的錯誤... – SWA

+0

@Vladimir我已經將子查詢移出SELECT,作爲參考EP中的內部可能是問題 –

+0

是的,明白了,非常感謝。現在我有WHERE子句的另一個問題。它沒有顯示出與office_id = NULL ... – SWA

1

你不能在其範圍內引用行內選擇表。 我建議使用自然連接,如:因爲你想加入到EP表Org_structure

Select Emp_ID,Emp_Name,Dep_Name,Office_Name,Pos_Name 
From Employees e 
Join Emp_positions ep on ep.Emp_ID = e.Emp_ID 
Join Org_Structue os on os.Chain_ID = ep.Chain_ID 
Join Departments d on d.Dep_ID = os.Dep_ID 
Join Offices o on o.office_ID = os.Office_ID 
Join Positions p on p.Pos_ID = os.Pos_ID 
Where (E.emp_name LIKE '%'[email protected]+'%' OR @empName IS NULL) 
    AND 
    (OS.dep_id = @empDepID OR @empDepID IS NULL) 
    AND 
    (OS.office_id = @empOfficeID OR @empOfficeID IS NULL) 
    AND 
    (EP.pos_id = @empPosID OR @empPosID IS NULL) 
+0

是的,我已經試過這個,它的工作原理,謝謝!現在我遇到** WHERE **的問題。它不顯示office_id = NULL的員工。 – SWA

1

您的查詢就會失敗,但這張EP是括號內。這是不允許的。請嘗試以下內容

ALTER PROCEDURE FilterEmpList 
@empName nvarchar(250) = null, 
@empDepID int = null, 
@empOfficeID int = null, 
@empPosID int = null  
AS 
    BEGIN 
SELECT 
E.emp_id AS 'emp_id', 
E.emp_name AS 'emp_name',  
(SELECT pos_name FROM Positions WHERE pos_id=EP.pos_id) AS 'pos_name', 
(SELECT dep_name FROM Departments WHERE dep_id=OS.dep_id) AS 'dep_name', 
(SELECT office_name FROM Offices WHERE office_id=OS.office_id) AS 'office_name' 
FROM 
    Org_Structure OS 
    INNER JOIN Emp_Positions EP ON OS.chain_id=EP.chain_id 
    INNER JOIN Employees E ON EP.emp_id=E.emp_id  
WHERE 
(E.emp_name LIKE '%'[email protected]+'%' OR @empName IS NULL) 
AND 
(OS.dep_id = @empDepID OR @empDepID IS NULL) 
AND 
(OS.office_id = @empOfficeID OR @empOfficeID IS NULL) 
AND 
(EP.pos_id = @empPosID OR @empPosID IS NULL) 
END 
2

內部連接/連接存在問題。在看到th查詢和ERD後,我假設你正在尋找這個,

ALTER PROCEDURE FilterEmpList 
    @empName nvarchar(250) = null, 
    @empDepID int = null, 
    @empOfficeID int = null, 
    @empPosID int = null  
AS 
BEGIN 
SELECT 
    E.emp_id AS 'emp_id', 
    E.emp_name AS 'emp_name',  
    (SELECT pos_name FROM Positions WHERE pos_id=EP.pos_id) AS 'pos_name', 
    (SELECT dep_name FROM Departments WHERE dep_id=OS.dep_id) AS 'dep_name', 
    (SELECT office_name FROM Offices WHERE office_id=OS.office_id) AS 'office_name' 
FROM 
    Org_Structure OS 
INNER JOIN Emp_Positions EP ON EP.ChainId == Emp_Positions.ChainId 
INNER JOIN Employees E ON EP.emp_id=E.emp_id  
WHERE 
    (E.emp_name LIKE '%'[email protected]+'%' OR @empName IS NULL) 
    AND 
    (OS.dep_id = @empDepID OR @empDepID IS NULL) 
    AND 
    (OS.office_id = @empOfficeID OR @empOfficeID IS NULL) 
    AND 
    (EP.pos_id = @empPosID OR @empPosID IS NULL) 
END 

也試圖使別名更易讀。

感謝

0

喜你檢查你的表EMP_Positions存在?

+0

是的,它存在。 – SWA