I am trying to execute this code below. It is a simplified example of the actual code I have to make, so I know that it is useless to loop in such a way. However, I need to look and union select statements in SQL Server. When I try to run this query I get an error:
Incorrect syntax near the keyword 'END'.
Any ideas?
  DECLARE @position INT
    SET @position = -1
    WHILE(@position < 1)
    BEGIN
    SELECT * FROM mytable
    UNION ALL
    END
 SELECT * FROM mytable
                Instead of trying to use UNION I would use a temp table or temp table variable to merge the result sets
CREATE TABLE #Temp
(
  <COLUMNS>
)
 DECLARE @position INT
 SET @position = -1
 WHILE(@position < 1)
 BEGIN
    INSERT INTO #Temp (<COLUMNS>)
    SELECT * FROM mytable
    SET @position = @position + 1
 END
 SELECT * FROM #Temp
                        That query makes no sense.  To use UNION, you need to be selecting from a second table.
SQL Server essentially sees:
SELECT * FROM mytable      
UNION ALL 
With no second table after the UNION.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With