在两个表相关时获取数据

时间:2014-05-01 06:26:17

标签: sql sql-server-2008 sql-server-2008-r2

我创建了两个表,如图

所示

enter image description here

我想在单个存储过程中检索名称地址和主题名称

我已将存储过程编写为

ALTER proc [dbo].[prStudentFetchALL]
as
Select StudentId, Name, Address, SubjectId from [tbStudent] 

我想检索SubjectName而不是SubjectId

请帮帮我!!!

1 个答案:

答案 0 :(得分:1)

您需要JOIN这些表:

ALTER proc [dbo].[prStudentFetchALL]
as
Select S.StudentId, S.Name, S.Address, Sub.SubjectName 
from tbStudent S JOIN
     tbSubject Sub ON S.SubjectId =Sub.SubjectId 

w3schoolsblog.sqlauthority了解JOIN的详情。