我有两张表dbo.tbRank
和dbo.tbStaff
dbo.Rank
我有RankID
和RankName
dbo.Staff
我有StaffID, StaffSNo, StaffRankID, StaffName, StaffGender
如何RankID
tbStaff
中的RankName
tbRank
与tbStaff
相同?
非常感谢任何帮助。谢谢。
以下是ALTER PROCEDURE [dbo].[selectStaff]
@sno as varchar(10)
AS
BEGIN
SET NOCOUNT ON;
-- Insert statements for procedure here
if @sno = ''
begin
select
StaffID, StaffSNo as 'Staff Service No.', StaffRankID,
StaffName as 'Name',
case StaffGender
when 'f' then 'female' when 'm' then 'male'
end as 'Gender'
from tbStaff
end
else
begin
select
StaffID, StaffSNo, StaffRankID, StaffName, StaffGender
from tbStaff
where StaffSNo = @sno
end
END
的存储过程:
{{1}}
答案 0 :(得分:0)
根据您的需要使用左连接或内连接。
select
a.StaffID, a.StaffSNo as 'Staff Service No.', a.StaffRankID,
a.StaffName as 'Name',
case a.StaffGender
when 'f' then 'female' when 'm' then 'male'
end as 'Gender' , b.RankName
from tbStaff a
left outer join Rank b on b.RankID = a.StaffRankID
答案 1 :(得分:0)
尝试以下查询
ALTER PROCEDURE [dbo].[selectStaff]
@sno AS VARCHAR(10)
AS
BEGIN
IF LTRIM(RTRIM(@sno)) = ''
BEGIN
SELECT
tST.StaffID,
tST.StaffSNo AS 'Staff Service No.',
tST.StaffRankID,
tRK.RankName,
tST.StaffName AS 'Name',
CASE tST.StaffGender
WHEN 'f' THEN 'female' WHEN 'm' THEN 'male'
END AS Gender
FROM tbStaff tST
LEFT JOIN tbRank tRK ON tRK.RankID = tST.StaffRankID
END
ELSE
BEGIN
SELECT
tST.StaffID, tST.StaffSNo, tST.StaffRankID, tRK.RankName, tST.StaffName, tST.StaffGender
FROM tbStaff tST
LEFT JOIN tbRank tRK ON tRK.RankID = tST.StaffRankID
WHERE tST.StaffSNo = @sno
END
END
根据您的需要更改联接LEFT或INNER
答案 2 :(得分:0)
这就是我做的。它奏效了。感谢所有帮助过的人。非常感谢您的帮助。
ALTER PROCEDURE [dbo]。[selectStaff] @sno as varchar(10) 如 开始 设置NOCOUNT ON; if @sno ='' 开始 选择 tbStaff.StaffID, tbStaff.StaffSNo为'服务号', tbStaff.StaffRankID, tbRank.RankName为Rank, tbStaff.StaffName为'Name', case tbStaff.StaffGender 当'f'然后'女' 当'm'然后'男' 结束为'性别'
from tbStaff
inner join tbRank on tbRank.RankID = tbStaff.StaffRankID
end
else
begin
select
tbStaff.StaffID,
tbStaff.StaffSNo as 'Service No.',
tbStaff.StaffRankID,
tbRank.RankName as Rank,
tbStaff.StaffName as Name,
tbStaff.StaffGender from tbStaff
inner join tbRank on tbRank.RankID = tbStaff.StaffRankID
where StaffSNo=@sno
end
END *强调文字*