question

asmagh-9402 avatar image
0 Votes"
asmagh-9402 asked MelissaMa-msft commented

display one result with while in sql

hello ,


please i'm trying this code in sql and i want the result to be in one table not in different table
131898-image.png


sql-server-transact-sql
image.png (310.0 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @asmagh-9402,

Could you please validate all the answers so far and provide any update?

Please remember to accept the answers if they helped. Your action would be helpful to other users who encounter the same issue and read this thread. 

Thank you for understanding!

Best regards,
Melissa

0 Votes 0 ·
OlafHelper-2800 avatar image
0 Votes"
OlafHelper-2800 answered

You can use the Set Operators - UNION (Transact-SQL) to concatenate the result of two queries.
But for this it's mandatory that both (or more) queries returns the same count of columns and data types.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

OlafHelper-2800 avatar image
0 Votes"
OlafHelper-2800 answered

display one result with while in sql

I missed your WHILE loop; that's when one post images instead of code.

Why a loop?
You can insert the query result into a temp table or table variable and query the result at the end of your loop.




5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MelissaMa-msft avatar image
0 Votes"
MelissaMa-msft answered

Hi @asmagh-9402,

Welcome to Microsoft Q&A!

It is recommended for you to post the complete query together with CREATE TABLE statements for your tables together with INSERT statements with sample data instead of snapshot.

You could have a try to create one template table and store all results under loop into this template table.

Please refer below example:

 DROP TABLE IF EXISTS #temp
    
 create table #temp
 (ADATE DATE,ID INT,DATEE DATE)
    
 declare @i date
 set @i='2021-01-01'
 while (@i<='2021-01-31')
 begin
 insert into #temp
 select @i,id, convert(date,CONCAT...
 from dbo.ods_arrangment arr
 where convert...
 set @i=DATEADD(day,7,@i)
 end
    
 select * from #temp

Since your snapshot is not a complete one, please fulfill above ellipsis part with your actual one.

Best regards,
Melissa


If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.