We help IT Professionals succeed at work.

Cycle through list run insert statement based on value from current row of list

117 Views
Last Modified: 2018-03-15
I am trying to write  SQL Script that cycles through a column of values and performs an insert based on the value of the row. I have some written below.  I am struggling with how to cycle  through the list while running the insert statement for each siteid value.

create table #mytable
(isiteid int);

insert into #mytable values(20);
insert into #mytable values(291);
insert into #mytable values(347);
insert into #mytable values(469);
insert into #mytable values(473);
insert into #mytable values(635);
insert into #mytable values(722);
insert into #mytable values(729);
insert into #mytable values(754);
insert into #mytable values(760);
insert into #mytable values(967);
insert into #mytable values(1188);
insert into #mytable values(1390);
insert into #mytable values(1463);
insert into #mytable values(1489);

declare @isiteid int
declare @icnt int

select @icnt= count(*) from #mytable

--everything works fine until the line below
select @isiteid = top 1 isiteid from #mytable
 
while @icnt>0

insert into DMRepository
(iSiteId,vchDocName,iDocTypeId,vchSubject,vchMimeType,chUser,vchLogoPath,vchImgPath,
txtLitContent,iEmailType,iFaxType,iPrintType,tiRecordStatus,chUpdateBy,dtUpdateDate,iSurveyId,iAutoType,iTrackingEmail,DMRepositoryDocumentSourceId, OemLocked)
values
(@iSiteId,'Test',113128,’Test','text/html','fsjvera','','',
'Hello ',
1,1,0,1,'fsjvera',getdate(),0,1,1,2,0)

Delete top 1 from #mytable
Set @icnt= @icnt-1

end

Please let me know if any additional information is needed.
Comment
Watch Question

Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.