获取值的MS SQL存储过程
我有三张桌子获取值的MS SQL存储过程,sql,sql-server,Sql,Sql Server,我有三张桌子 Staff table: EmpId CandidateId ------------------------ 1 2 Candidate table: CandidateId Firstname Last name CountryId PassportCountry -------------------------------------------------------------------- 1
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
显示候选人详细信息。我只有一个国家
表,并将该表用于国家
、护照港口国家。因此,当我得到候选值时,我需要得到国家名称
如何编写存储过程以获取候选详细信息。我不擅长sql。你们能帮我一下吗。提前谢谢
嗨,我尝试了下面的脚本来获取国家名称和护照国家名称。我可以得到国家的名字,但不能得到护照上的国家
SELECT
FirstName,
LastName,
PassportCountry,
Country.CountryName as Country
from Candidate
inner join Country
on country.CountryId=candidate.country
where CandidateId=@CandidateId;
这会让你走上正确的方向
基本上,因为要引用两次国家表,所以需要加入两次
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 s.EmpId=@empID;
让我们看看你尝试了什么。你尝试了什么SQL?结果如何?嗨,我尝试了下面的脚本来获取国家名称和护照国家名称。我可以得到国家名称,但不能得到护照上的国家。回答如下。有一件事我注意到,您的工作人员
表未被使用……您好,谢谢您的帮助,工作正常。用户将在查询字符串中传递EmpId,我需要根据EmpId显示候选人详细信息。你能帮我吗。提前感谢。已更新为使用empID
。