display one result with while in sql

asma gh 21 Reputation points
2021-09-14T10:45:50.99+00:00

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

Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,582 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Olaf Helper 42,571 Reputation points
    2021-09-14T11:27:26.147+00:00

    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.

    0 comments No comments

  2. Olaf Helper 42,571 Reputation points
    2021-09-14T11:38:04.377+00:00

    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.

    0 comments No comments

  3. MelissaMa-MSFT 24,176 Reputation points
    2021-09-15T01:14:17.627+00:00

    Hi @asma gh ,

    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.

    0 comments No comments