1

我从查询中得到多个值。

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 循环语法?

4

2 回答 2

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

通常一个连接条件涉及两个表。

我建议阅读各种连接。

于 2012-08-13T04:51:42.110 回答
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
于 2012-08-13T04:54:36.987 回答