SQL Query help

Hi,

How can Insert the same record until I find the next value in SQL? Here is my sample code/data.

Blockquote

DECLARE @test as table
(
id int IDENTITY(1,1),
code varchar(20),
result varchar(20)
);

INSERT INTO @test (code,result)
VALUES
('R235','Good'),
('R236',''),
('R236',''),
('R235','Failed'),
('R238',''),
('R239','Verified'),
('R239',''),
('R235','')

SELECT * FROM @test t

Here what I want.

Final output

id,code,result

1,R235,Good

2,R236,Good

3,R236,Good

4,R235,Failed

5,R238,Failed

6,R239,Verified

7,R239,Verified

8,R235,Verified

Thanks for help!

Your question is not clear. What do you mean by

Insert the same record until I find the next value

You have issue with "inserting" or "selecting" the rows?

hi

hope this helps

i used recursive cte approach

;WITH  cte_rec as 
  (
    select * from @test where id = 1 
    union all
    select e.id ,e.code ,case when e.result  = '' then r.result else e.result end  
	  from  @test  e  inner join  cte_rec r on e.id = r.id+1 
  )
select * from cte_rec

image

Thank you, harishgg1 for you help!