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
You need a second set to UNION on. The while loop doesn’t hold on to the selected set and know it the next time through. What you’re trying to accomplish is basically select all the data from mytable then union it again on the same set…n times. Which really doesn’t make sense. Remember that SQL is set based, so select * From mytable will select every record in there. You also never state what you’re trying to accomplish.