迭代查询返回的记录 [英] Iterate through records returned by a query
问题描述
我有一个返回少量记录的查询。查询结果有一个ACCTID列。现在,对于每条记录,我需要从其他表中获取ACCTID和获取记录。我尝试用C#做但是使用循环,但这需要花费很多时间。请教我如何在SQL服务器端执行相同操作。
查询概述如下:
主要查询:
选择 NAME,CANCEL_DATE ...,ACCTID 来自 Table1;
我需要为 ACCTID
做什么现在是从<... c $ c的其他两个表中获取数据> ACCTID 从主查询中获取
:
选择 * 来自 table2 其中 ACCTID = @ ACCTID
选择 * 来自 table3 其中 ACCTID = @ ACCTID
最后,我需要返回从上面两个查询中获取的所有记录/>
我尝试了什么:
目前我正在尝试使用光标来完成它
使用JOIN:
SELECT a.Name,a.Cancel_Date,b。*,c。* FROM 表1 a
JOIN 表2 b ON a.ACCID = b.ACCID
JOIN 表3 c ON a.ACCID = c.ACCID
WHERE a.ACCID = @ ACCID 跨度>
I have a query which returns few records. The Query result has a column 'ACCTID'. Now for each record i need to take the ACCTID and fetch record from other table. I tried it to do in C# but using loop, but it is taking lot of time. Pls suggest me how to do the same in SQL server side.
Overview of query is like below:
Main query:
Select NAME, CANCEL_DATE..., ACCTID from Table1;
What i need to do for ACCTID
now is fetch data from two other tables for ACCTID
fetched from above main query
:
select * from table2 where ACCTID=@ACCTID
select * from table3 where ACCTID=@ACCTID
In the end i need to return all the records fetched from above two queries.
What I have tried:
currently I am trying to use cursor to get it done
Use a JOIN:
SELECT a.Name, a.Cancel_Date, b.*, c.* FROM Table1 a JOIN Table2 b ON a.ACCID = b.ACCID JOIN Table3 c ON a.ACCID = c.ACCID WHERE a.ACCID = @ACCID
这篇关于迭代查询返回的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!