我在数据库中有四个表。
我想编写存储过程,返回每个Research and Researcher Name和Supervisor Names,以便将结果放入datagridview。
答案 0 :(得分:1)
您没有指定DBMS,所以我假设PostgreSQL:
create or replace function get_researchers()
returns table(research_name, researchers, supervisors)
language sql
AS
$body$
select r.resname as research_name,
string_agg(p.firstname||' '||p.lastname, ',') as researchers,
string_agg(sp.firstname||' '||sp.lastname, ',') as supervisors
from research r
join person_research pr on pr.resid = r.resid
join person p on pr.perid = p.perid,
join supervisors s on s.resid = r.resid
join person sp on sp.perid = s.perid
group by r.resname
order by r.resname;
$body$
答案 1 :(得分:0)
for SQL server:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE dbo.test
AS
BEGIN
SET NOCOUNT ON;
SELECT R.resname as researchname
, (p1.firstname + p1.lastname) as researcher
, (p2.firstname + p2.lastname) as supervisor
FROM Research R
JOIN person_research PR
ON R.Resid = PR.Resid
JOIN person p1
on PR.Perid = p1.Perid
JOIN supervisors s
on R.Resid = s.Resid
JOIN person p2
on PR.Perid = p2.Perid
END
GO