2014-06-19 81 views
-4

我有3個表MS SQL存儲過程來獲取值

Staff table: 

EmpId CandidateId 
------------------------ 
    1   2 


Candidate table: 

CandidateId Firstname Last name CountryId PassportCountry 
-------------------------------------------------------------------- 
    1   Mark  Antony   2   3   
    2   Joy   terry   1   3 

Country: 

CountryId  Name 
--------------------------- 
    1   USA 
    2   UK 
    3   Australia 

用戶將通過EmpId的查詢字符串,我需要根據empId展現候選人的詳細信息。我只有一個country表,並將該表用於country,passportport國家。所以我得到country name時,我得到候選人的價值。

如何編寫存儲過程以獲取候選詳細信息。我不擅長SQL。你們能幫我解決這個問題嗎?提前致謝。

嗨我試了下面的腳本來獲得國家名稱和護照國家名稱。我可以得到國家名稱,但不是護照國家。

SELECT 
FirstName, 
LastName, 
PassportCountry, 
Country.CountryName as Country 
from Candidate 
inner join Country 
on country.CountryId=candidate.country 
where [email protected]; 
+1

向我們展示您試過的東西。 – JiggsJedi

+0

你試過了什麼SQL?結果是什麼? –

+0

嗨,我試了下面的腳本來獲得國家名稱和護照國家名稱。我可以得到國家名稱,但不是護照國家。 –

回答

0

這應該讓你朝着正確的方向前進。

基本上,由於您引用國家表兩次,您需要加入它兩次。

declare @staff table (EmpId int, CandidateId int) 
insert into @staff values (1, 2) 

declare @Candidate table (CandidateId int, Firstname nvarchar(50), Lastname nvarchar(50), CountryId int, PassportCountry int) 
insert into @Candidate 
values (1, 'Mark', 'Antony', 2, 3), 
     (2, 'Joy', 'Terry', 1, 3) 

declare @Country table (CountryId int, Name nvarchar(50)) 
insert into @Country 
values (1, 'USA'), 
     (2, 'UK'), 
     (3, 'Australia') 


declare @empID int 
set @empID = 1 

SELECT 
FirstName, 
LastName, 
t2.Name as PersonCountry, 
t3.Name as PassportCountry 
from @staff s 
inner join @Candidate t1 on s.CandidateId = t1.CandidateId 
inner join @Country t2 on t1.CountryId=t2.CountryId 
inner join @Country t3 on t1.PassportCountry=t3.CountryId 
where [email protected]; 
+0

嗨謝謝你的幫助它的工作正常。用戶將通過查詢字符串中的EmpId我需要根據EmpId顯示候選人詳細信息。你能幫我解決這個問題嗎?提前致謝。 –

+0

更新爲使用'empID'。 – JiggsJedi