Link to home
Start Free TrialLog in
Avatar of jverasql

asked on

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

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
txtLitContent,iEmailType,iFaxType,iPrintType,tiRecordStatus,chUpdateBy,dtUpdateDate,iSurveyId,iAutoType,iTrackingEmail,DMRepositoryDocumentSourceId, OemLocked)
'Hello ',

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


Please let me know if any additional information is needed.
Avatar of jverasql

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial