Click here to Skip to main content
15,887,822 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
<HELLO ALL,
HOW TO MAKE UNION OF "2 IF CONDITION" QUERIES?

FOR EXAMPLE:

IF(J=J)
BEGIN
SELECT ID,* FROM TABLE1
END

UNION

IF(I=I)
BEGIN
SELECT ID,* FROM TABLE2
END

WITH GROUPBY ID



PLS HELP.... ITS URGENT
Posted

This can be done by using a stored procedure where I and J are passed as parameters.
 
Share this answer
 
IF(I=I && J==J)
BEGIN
SELECT ID,* FROM TABLE1 UNION SELECT * FROM TABLE2 GROUP BY id
END

else
begin

IF(J=J)
BEGIN
SELECT ID,* FROM TABLE1 GROUPBY ID
END


else
begin
IF(I=I)
BEGIN
SELECT ID,* FROM TABLE2 GROUPBY ID
END
end

end


But be sure that table1 and table2 have equal number of expressions in it.
 
Share this answer
 
v2
If condition and union both are required then that can be done as shown below:


IF (J=J) THEN
IF (I=I) THEN
SELECT ID,* FROM TABLE1
UNION
SELECT ID,* FROM TABLE2
Group by ID

That is you can use nested if and then union those two queries required to perform union function. All the best.
 
Share this answer
 
v2
why don't try temp table, such as:

IF(J=J)
BEGIN
SELECT ID,* INTO #TEMPTBL1 FROM TABLE1
END

IF(I=I)
BEGIN
SELECT ID,* INTO #TEMPTBL2 FROM TABLE2
END

SELECT * FROM #TEMPTBL1
UNION
SELECT * FROM #TEMPTBL2
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900