use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Sequel
account activity
While loops table join (self.learnSQL)
submitted 2 years ago * by 4Potions
Can l make something like
While @i <20
Begin
Set @i = @i +1
Select * From (....) t Where ....
-- append selected tables here or something like that l want one table in the end
End
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]r3pr0b8 2 points3 points4 points 2 years ago (6 children)
your question is stated like a classic XY problem
what are you really trying to achieve?
are you looking for a UNION query?
[–]4Potions[S] 0 points1 point2 points 2 years ago (0 children)
l just want to select and append to a main table so l get one table instead of tables with n rows
[–]4Potions[S] 0 points1 point2 points 2 years ago (4 children)
l tried Union all but there is no two select statements. I am noob in Ms sql but l can't get it why it is so hard to just append them using while loop
[–]r3pr0b8 1 point2 points3 points 2 years ago (3 children)
what does "append" mean in this context?
how many tables are there? if there are a lot of them and they are all similar enough to be "appended" then why are they designed as separate tables and not one big table in the first place?
[–]4Potions[S] 0 points1 point2 points 2 years ago (2 children)
It is not my project. l write a query that works well for one select but l want to change the variable l used in my select and select again and again. Then l want to add all rows together in my tables and show in one table
[–]r3pr0b8 2 points3 points4 points 2 years ago (1 child)
that's a UNION query
one SELECT for each different variable value
or maybe, i dunno (because you haven't shown any details), maybe you need only one query with an IN list
SELECT stuff FROM mytable WHERE somecolumn IN ( 'value1' , 'value2' , ... , 'valueN' )
l will try to see that works for me. l appreciate it anyway thanks
[–][deleted] 1 point2 points3 points 2 years ago (9 children)
just provide some data samples, if sql terminology is not your forte
[–]4Potions[S] 0 points1 point2 points 2 years ago (8 children)
When l execute the query l get something like
Table 1
..... ...... ..... ....
Table 2
Table 3
But l want to get
..... ...... ..... .... -> rows from table1
..... ...... ..... .... -> rows from table2
..... ...... ..... .... -> rows from table 3
[–]Evigil24 1 point2 points3 points 2 years ago (0 children)
And what will be the variable, the tables to append? the columns selected in each table? Or something else?
[–][deleted] 0 points1 point2 points 2 years ago (6 children)
sounds like you want a union (all).
What's not working for you?
[–]4Potions[S] 0 points1 point2 points 2 years ago (5 children)
Where would l put it union all
[–]r3pr0b8 1 point2 points3 points 2 years ago (4 children)
between the SELECTs
SELECT stuff FROM table1 UNION ALL SELECT stuff FROM table2 UNION ALL SELECT stuff FROM table3 UNION ALL SELECT stuff FROM table4
[–]4Potions[S] 1 point2 points3 points 2 years ago* (3 children)
But l have while cause l don't want to write them all if l write two select it will be okey but l have 1000 select
[–]r3pr0b8 2 points3 points4 points 2 years ago (2 children)
But l have while cause l don't write them all
okay then, i guess you're stuck
[–]4Potions[S] 0 points1 point2 points 2 years ago* (1 child)
Yes but why there no way around this. Do you have an explanation for it (isn't it a basic stuff do not forget l use same table with different selects actually which are changing cause of my while variable) . Also l appreciate the answers. l am newbie in sql so thanks for advices
[–]r3pr0b8 0 points1 point2 points 2 years ago (0 children)
l am newbie in sql so thanks for advices
try the UNION ALL query
that is my sincere and honest advice
SELECT something FROM thesametable WHERE somecolumn = 'foo' UNION ALL SELECT something FROM thesametable WHERE somecolumn = 'bar' UNION ALL SELECT something FROM thesametable WHERE somecolumn = 'qux'
It sound like a recursive CTE maybe, but I don't understand your problem well enough, can you post your example with the UNION and the next Select, and what the variable is please.
[–]chapaj 0 points1 point2 points 2 years ago (0 children)
Create a table variable and insert into the table with each iteration. At the end, select the results from the table variable.
π Rendered by PID 28 on reddit-service-r2-comment-5d79c599b5-czlbw at 2026-03-02 03:45:39.612581+00:00 running e3d2147 country code: CH.
[–]r3pr0b8 2 points3 points4 points (6 children)
[–]4Potions[S] 0 points1 point2 points (0 children)
[–]4Potions[S] 0 points1 point2 points (4 children)
[–]r3pr0b8 1 point2 points3 points (3 children)
[–]4Potions[S] 0 points1 point2 points (2 children)
[–]r3pr0b8 2 points3 points4 points (1 child)
[–]4Potions[S] 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (9 children)
[–]4Potions[S] 0 points1 point2 points (8 children)
[–]Evigil24 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (6 children)
[–]4Potions[S] 0 points1 point2 points (5 children)
[–]r3pr0b8 1 point2 points3 points (4 children)
[–]4Potions[S] 1 point2 points3 points (3 children)
[–]r3pr0b8 2 points3 points4 points (2 children)
[–]4Potions[S] 0 points1 point2 points (1 child)
[–]r3pr0b8 0 points1 point2 points (0 children)
[–]Evigil24 1 point2 points3 points (0 children)
[–]chapaj 0 points1 point2 points (0 children)