我目前在存储过程中使用连接来输出不同表中的元素。一个积极的例子在T-SQL中使用多个结果集并结合连接
select a.*, b.*, c.*, d.*, e.*, f.* from tableA a
join tableB b on a.id = b.foreignid
join tableC c on b.id = c.foreignid
join tableD d on c.id = d.foreignid
join tableE e on d.id = e.foreignid
join tableF f on e.id = f.foreignid
where a.id = 1
它变得非常不方便在我的C#代码映射输出到实体时,一起工作,因为我要保持大量的样板代码。 相反,我会研究使用多个结果集,以便我可以将每个结果集映射到代码中的对象类型。 但是,如果我的情况下不同的结果会彼此相关,我将如何去实现这一目标?我所能找到的所有例子都是围绕从不同的表格中选择数据与我的外键无关的。如果我要输出中我的成绩在多个结果我能想出的唯一事情是这样的
select a.* from tableA
where a.id = 1
select b.* from tableB
join tableA a on a.id = b.foreignid
where a.id = 1
select c.* from tableC
join tableB b on b.id = c.foreignid
join tableA on a.id = b.foreginid
where a.id = 1
select d.* from tableD
join tableC c on c.id = d.foreignid
join tableB b on b.id = c.foreignid
join tableA a on a.id = b.foreignid
where a.id = 1
select e.* from tableE
join tableD d on d.id = e.foreignid
join tableC c on c.id = d.foreignid
join tableB b on b.id = c.foreignid
join tableA a on a.id = b.foreignid
where a.id = 1
select f.* from tableF
join tableE e on e.id = f.foreignid
join tableD d on d.id = e.foreignid
join tableC c on c.id = d.foreignid
join tableB b on b.id = c.foreignid
join tableA a on a.id = b.foreignid
where a.id = 1
但这不是清洁,多了很多ineffecient(我会想,因为有很多更加入语句) 是否有可能以这种方式使用多个结果集我试图?我只是不知道如何在存储过程中编写sql语句,而无需像示例中那样按结果集进行大量连接。并配备了目前的解决方案,我得到列的爆炸,因为我加入他们一起
我知道如何消费多个结果在我的代码,如果我能我会使用ORM。但是我们所有的数据库访问都使用存储过程。我确实使用linq to sql来使用我的存储过程,但这实际上并不能给我一个ORM的全部功能。我的问题是关于当实际需要加入我的数据时,是否可以在存储过程中返回多个结果集 – Xorandor
我不确定我是否理解,是否需要多个结果集,在SP中是否有多个selectt语句。如果你需要加入然后加入。还是你想以一对多的方式将你创建的对象从结果集中链接到一起,就像使用外键的数据库一样? – gjvdkamp
想要实现的更多示例代码以及遇到的问题将对此有所帮助。 – gjvdkamp