If you need a WHILE loop for some reasons, then also consider a cursor. For example:
declare c cursor for
select d.BookdetailsId, d.BookId, d.PublishersPlaces, d.[Status]
from #BookDetails d
inner join #mainbooks b on b.BookId = d.BookId
where d.BookId in
(
select BookId
from #BookDetails
where [Status] is not null
)
open c
while 0=0
begin
declare @bookDetailsId int
declare @Id int
declare @publisherPlaces nvarchar(50)
declare @status nvarchar(50)
fetch next from c into @bookDetailsId, @Id, @publisherPlaces, @status
if @@FETCH_STATUS <> 0 break
insert #BookHaveGeneralStatus
select @bookDetailsId, @Id, @publisherPlaces, @status
end
close c deallocate c
select *
from #BookHaveGeneralStatus
order by BookgeneralId
An INSERT without loop:
insert #BookHaveGeneralStatus
select d.BookdetailsId, d.BookId, d.PublishersPlaces, d.[Status]
from #BookDetails d
inner join #mainbooks b on b.BookId = d.BookId
where d.BookId in
(
select BookId
from #BookDetails
where [Status] is not null
)