Question : UNION multiple SELECT statements using IF/THEN

I'm trying to modify a stored procedure to include the ability of running multiple select statements depending on the value of a variable.  Unfortunately, it appears that you can't UNION the statements if you're using IF before them -- either that or I've simply got the syntax wrong.  I also looked into using CASE but I don't know if it's possible to make that work in this way either.  Can anyone show me what I'm doing wrong?

Thanks very much!
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
ALTER PROCEDURE [dbo].[sproc_processgroupstep_users_select] 
	@ProcessGroupID int,
	@SuiteID int,
	@UserID int
	
AS 
SET NOCOUNT ON; 
DECLARE 	
	@COID varchar(50),
	@Position1 int,
	@Position2 int,
	@Position3 int,
	@Position4 int 
SET @COID = (
	SELECT tblSpace.coID
	FROM tblSpace, tblSuite
	WHERE tblSuite.suiteID = @SuiteID AND tblSuite.spaceID = tblSpace.spaceID
	) 
SELECT TOP 1 @Position1 = processGroupStepAssignTo1, @Position2 = processGroupStepAssignTo2, @Position3 = processGroupStepAssignTo3, @Position4 = processGroupStepAssignTo4
FROM tblProcessGroupStep
WHERE processGroupID = @ProcessGroupID
ORDER BY processGroupStepNumber ASC; 
IF @Position1 = 0
		BEGIN
			SELECT tblUsers.userID, userFName, userLName, teamPositionName, 0 as groupOrder
			FROM tblTeamMember
			JOIN tblTeamPosition ON tblTeamMember.teamPositionID = tblTeamPosition.teamPositionID
			JOIN tblUsers ON tblTeamMember.userID = tblUsers.userID
			WHERE tblTeamMember.userID = @UserID AND tblTeamMember.coID = @COID
		END
	IF @Position1 <> 0
		BEGIN
			SELECT tblUsers.userID, userFName, userLName, teamPositionName, 0 as groupOrder
			FROM tblTeamMember
			JOIN tblTeamPosition ON tblTeamMember.teamPositionID = tblTeamPosition.teamPositionID
			JOIN tblUsers ON tblTeamMember.userID = tblUsers.userID
			WHERE tblTeamMember.teamPositionID = @Position1 AND tblTeamMember.coID = @COID
		END 
UNION 
	IF @Position2 = 0
		BEGIN
			SELECT tblUsers.userID, userFName, userLName, teamPositionName, 1 as groupOrder
			FROM tblTeamMember
			JOIN tblTeamPosition ON tblTeamMember.teamPositionID = tblTeamPosition.teamPositionID
			JOIN tblUsers ON tblTeamMember.userID = tblUsers.userID
			WHERE tblTeamMember.userID = @UserID AND tblTeamMember.coID = @COID
		END
	IF @Position2 <> 0
		BEGIN
			SELECT tblUsers.userID, userFName, userLName, teamPositionName, 1 as groupOrder
			FROM tblTeamMember
			JOIN tblTeamPosition ON tblTeamMember.teamPositionID = tblTeamPosition.teamPositionID
			JOIN tblUsers ON tblTeamMember.userID = tblUsers.userID
			WHERE tblTeamMember.teamPositionID = @Position2 AND tblTeamMember.coID = @COID
		END 
UNION 
BEGIN
	IF @Position3 = 0
		BEGIN
			SELECT tblUsers.userID, userFName, userLName, teamPositionName, 2 as groupOrder
			FROM tblTeamMember
			JOIN tblTeamPosition ON tblTeamMember.teamPositionID = tblTeamPosition.teamPositionID
			JOIN tblUsers ON tblTeamMember.userID = tblUsers.userID
			WHERE tblTeamMember.userID = @UserID AND tblTeamMember.coID = @COID
		END
	IF @Position3 <> 0
		BEGIN
			SELECT tblUsers.userID, userFName, userLName, teamPositionName, 2 as groupOrder
			FROM tblTeamMember
			JOIN tblTeamPosition ON tblTeamMember.teamPositionID = tblTeamPosition.teamPositionID
			JOIN tblUsers ON tblTeamMember.userID = tblUsers.userID
			WHERE tblTeamMember.teamPositionID = @Position3 AND tblTeamMember.coID = @COID
		END
END 
UNION 
BEGIN
	IF @Position4 = 0
		BEGIN
			SELECT tblUsers.userID, userFName, userLName, teamPositionName, 3 as groupOrder
			FROM tblTeamMember
			JOIN tblTeamPosition ON tblTeamMember.teamPositionID = tblTeamPosition.teamPositionID
			JOIN tblUsers ON tblTeamMember.userID = tblUsers.userID
			WHERE tblTeamMember.userID = @UserID AND tblTeamMember.coID = @COID
		END
	IF @Position4 <> 0
		BEGIN
			SELECT tblUsers.userID, userFName, userLName, teamPositionName, 3 as groupOrder
			FROM tblTeamMember
			JOIN tblTeamPosition ON tblTeamMember.teamPositionID = tblTeamPosition.teamPositionID
			JOIN tblUsers ON tblTeamMember.userID = tblUsers.userID
			WHERE tblTeamMember.teamPositionID = @Position4 AND tblTeamMember.coID = @COID
		END
END 
SET NOCOUNT OFF;

Answer : UNION multiple SELECT statements using IF/THEN

This would do
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
ALTER PROCEDURE [dbo].[sproc_processgroupstep_users_select] 
        @ProcessGroupID int,
        @SuiteID int,
        @UserID int
        
AS 
SET NOCOUNT ON; 
DECLARE         
        @COID varchar(50),
        @Position1 int,
        @Position2 int,
        @Position3 int,
        @Position4 int 
SET @COID = (
        SELECT tblSpace.coID
        FROM tblSpace, tblSuite
        WHERE tblSuite.suiteID = @SuiteID AND tblSuite.spaceID = tblSpace.spaceID
        )  
SELECT TOP 1 @Position1 = processGroupStepAssignTo1, @Position2 = processGroupStepAssignTo2, @Position3 = processGroupStepAssignTo3, @Position4 = processGroupStepAssignTo4
FROM tblProcessGroupStep
WHERE processGroupID = @ProcessGroupID
ORDER BY processGroupStepNumber ASC; 
IF @Position1 = 0
                BEGIN
                        SELECT tblUsers.userID, userFName, userLName, teamPositionName, 0 as groupOrder
                        INTO temp_table
                        FROM tblTeamMember
                        JOIN tblTeamPosition ON tblTeamMember.teamPositionID = tblTeamPosition.teamPositionID
                        JOIN tblUsers ON tblTeamMember.userID = tblUsers.userID
                        WHERE tblTeamMember.userID = @UserID AND tblTeamMember.coID = @COID
                END
        IF @Position1 <> 0
                BEGIN
                        SELECT tblUsers.userID, userFName, userLName, teamPositionName, 0 as groupOrder
                        INTO temp_table
                        FROM tblTeamMember
                        JOIN tblTeamPosition ON tblTeamMember.teamPositionID = tblTeamPosition.teamPositionID
                        JOIN tblUsers ON tblTeamMember.userID = tblUsers.userID
                        WHERE tblTeamMember.teamPositionID = @Position1 AND tblTeamMember.coID = @COID
                END  
        IF @Position2 = 0
                BEGIN
                        INSERT INTO temp_table(userID, userFName, userLName, teamPositionName, groupOrder)
                        SELECT tblUsers.userID, userFName, userLName, teamPositionName, 1 as groupOrder
                        FROM tblTeamMember
                        JOIN tblTeamPosition ON tblTeamMember.teamPositionID = tblTeamPosition.teamPositionID
                        JOIN tblUsers ON tblTeamMember.userID = tblUsers.userID
                        WHERE tblTeamMember.userID = @UserID AND tblTeamMember.coID = @COID
                END
        IF @Position2 <> 0
                BEGIN
                        INSERT INTO temp_table(userID, userFName, userLName, teamPositionName, groupOrder)
                        SELECT tblUsers.userID, userFName, userLName, teamPositionName, 1 as groupOrder
                        FROM tblTeamMember
                        JOIN tblTeamPosition ON tblTeamMember.teamPositionID = tblTeamPosition.teamPositionID
                        JOIN tblUsers ON tblTeamMember.userID = tblUsers.userID
                        WHERE tblTeamMember.teamPositionID = @Position2 AND tblTeamMember.coID = @COID
                END 
UNION 
BEGIN
        IF @Position3 = 0
                BEGIN
                        INSERT INTO temp_table(userID, userFName, userLName, teamPositionName, groupOrder)
                        SELECT tblUsers.userID, userFName, userLName, teamPositionName, 2 as groupOrder
                        FROM tblTeamMember
                        JOIN tblTeamPosition ON tblTeamMember.teamPositionID = tblTeamPosition.teamPositionID
                        JOIN tblUsers ON tblTeamMember.userID = tblUsers.userID
                        WHERE tblTeamMember.userID = @UserID AND tblTeamMember.coID = @COID
                END
        IF @Position3 <> 0
                BEGIN
                        INSERT INTO temp_table(userID, userFName, userLName, teamPositionName, groupOrder)
                        SELECT tblUsers.userID, userFName, userLName, teamPositionName, 2 as groupOrder
                        FROM tblTeamMember
                        JOIN tblTeamPosition ON tblTeamMember.teamPositionID = tblTeamPosition.teamPositionID
                        JOIN tblUsers ON tblTeamMember.userID = tblUsers.userID
                        WHERE tblTeamMember.teamPositionID = @Position3 AND tblTeamMember.coID = @COID
                END
END 
UNION 
BEGIN
        IF @Position4 = 0
                BEGIN
                        INSERT INTO temp_table(userID, userFName, userLName, teamPositionName, groupOrder)
                        SELECT tblUsers.userID, userFName, userLName, teamPositionName, 3 as groupOrder
                        FROM tblTeamMember
                        JOIN tblTeamPosition ON tblTeamMember.teamPositionID = tblTeamPosition.teamPositionID
                        JOIN tblUsers ON tblTeamMember.userID = tblUsers.userID
                        WHERE tblTeamMember.userID = @UserID AND tblTeamMember.coID = @COID
                END
        IF @Position4 <> 0
                BEGIN
                        INSERT INTO temp_table(userID, userFName, userLName, teamPositionName, groupOrder)
                        SELECT tblUsers.userID, userFName, userLName, teamPositionName, 3 as groupOrder
                        FROM tblTeamMember
                        JOIN tblTeamPosition ON tblTeamMember.teamPositionID = tblTeamPosition.teamPositionID
                        JOIN tblUsers ON tblTeamMember.userID = tblUsers.userID
                        WHERE tblTeamMember.teamPositionID = @Position4 AND tblTeamMember.coID = @COID
                END
END 
SELECT * from temp_table
SET NOCOUNT OFF;
Random Solutions  
 
programming4us programming4us