I have 3 tables like so
bug_groups
ID NAME DESCRIPTION
--------------------------------------
1 Group1 Some description
2 Group2 Some description
3 Group3 Some description
bugs
ID NAME DESCRIPTION BELONGSTO
--------------------------------------------------
1 Bug1 something 1
2 Bug2 something 1
3 Bug3 something 2
bug_comments
ID BUGID COMMENT
-----------------------------
1 1 something
2 1 something
3 2 something
4 2 something
5 3 something
I want to create a stored procedure that will get 2 result sets, one with all the bugs for a given bug group and one with all the comments for all of the bugs in the group. The BELONGSTO field specifies which group that the bug is a part of. So If I wanted to run this query, passing in 1 for the bug group ID I would expect this result
RESULT SET 1
ID NAME DESCRIPTION BELONGSTO
----------------------------------------------------
1 Bug1 something 1
2 Bug2 something 1
RESULT SET 2
ID BUGID COMMENT
------------------------------
1 1 something
2 1 something
3 2 something
4 2 something
I think I may need to use a temporary table but I’m unsure how to do this. This is what I have so far (which doesn’t work)
ALTER PROCEDURE bugs_getAllBugGroupData
@groupid int
AS
BEGIN
DECLARE @bugids TABLE (id int)
INSERT @bugids
SELECT id FROM bugs
WHERE belongsto = @groupid
SELECT * FROM bugs WHERE belongsto = @groupid
SELECT * FROM bugs_comments WHERE bugid IN (@bugids)
END
GO
The error message I get is “Must declare the scalar variable @bugids” at the last select statement.
This line
has to be
However you don’t need an auxiliary table. You can query directly using a join:
1.
2.