2014-06-19 83 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