My query is as below
declare @row_id int = 2
declare @last_row_id int =(select MAX(Row_ID) from dbo.Source)
create table #source (
Row_ID float null,
[Document] [nvarchar](255) NULL,
[ITEMCode] [nvarchar](255) NULL,
[Text] [nvarchar](255) NULL)
while(@row_id<=(@last_row_id))
begin
declare @Document nvarchar(255)
declare @itemcode nvarchar(255)
select @itemcode=ITEMCode,@Document=Document from dbo.Source where Row_ID=@row_id
if ((@itemcode='' or @itemcode is null ) )
select @itemcode=ITEMCode,@Document=Document from #source where Row_ID=@row_id-1
insert into #source
select Row_ID,@Document,@itemcode,[Text]
from dbo.Source where Row_ID=@row_id
print @row_id
set @row_id= @row_id+1
end
select * from #source
drop table #source
Presently my table has 347000 rows .its taking more than a hour to get me final output . how can this query made faster . Can anyone help ?
Requirement :
Source :
Row_ID Document ITEMCode Text
2 10223 20235 aaaa
3 bbbb
4 cccc
5 10278 202475 xxxx
6 yyyy
7 yyy
Output should be :
Row_ID Document ITEMCode Text
2 10223 20235 aaaa
3 10223 20235 bbbb
4 10223 20235 cccc
5 10278 202475 xxxx
6 10278 202475 yyyy
7 10278 202475 yyy
Even though above specified queries might work , I wrote a macro in excel which we quire fast n got the output in less than 5 min