我从查询中获取了多个值。
select l_t_id from MLTalukDetails join MLDistrictDetails on MLTalukDetails.L_D_Id=4
它返回13,14,15,16。现在我想一次取下这些值,并从这些值中得到细节,即
select Sc_Id from MSchoolDetails
where
MSchoolDetails.L_T_Id=@talukid and
MSchoolDetails.Y_Id=@yearid
我可以做这个场景。我如何在这里使用for循环语法?
答案 0 :(得分:1)
我从你的问题推断这是你真正想要的完整解决方案:
SELECT MSchoolDetails.*
FROM MLTalukDetails
INNER JOIN MSchoolDetails ON MTalukDetails.l_t_id = MSchoolDetails.L_T_Id
WHERE MLTalukDetails.L_D_ID = 4 AND MSchoolDetails.Y_Id=@yearid
通常一个有一个涉及两个表的连接条件。
我建议reading up加入各种联接。
答案 1 :(得分:0)
如果你想使用循环,请使用游标....这是我在存储过程中实现的解决方法....
CREATE PROCEDURE [dbo].[proc_GetSc_Id]
@yearid INT
AS
CREATE TABLE #Sc_Id(Sc_Id varchar(20))
BEGIN
DECLARE @talukid varchar(20)
declare c cursor For
select l_t_id from MLTalukDetails join MLDistrictDetails on MLTalukDetails.L_D_Id=4
open c
fetch next from c into @talukid
while(@@FETCH_STATUS=0)
BEGIN
insert into #Sc_Id(Sc_Id)
select Sc_Id from MSchoolDetails where MSchoolDetails.L_T_Id=@talukid and MSchoolDetails.Y_Id=@yearid
fetch next from c into @talukid
END
select * from #Sc_Id;---Here you got your final result.
close c
deallocate c
DROP TABLE #Sc_Id
END