Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Looping Insert Statement

Author  Topic 

sqllearner
Aged Yak Warrior

639 Posts

Posted - 2004-06-23 : 02:58:25
I have a table called company_areas

INSERT INTO company_areas VALUES ('Greg','STATE','AK',NULL,'SYSTEM',getdate(),NULL,NULL)

INSERT INTO company_areas VALUES ('jack','STATE','GA',NULL,'SYSTEM',getdate(),NULL,NULL)

This is the insert statement Iam using..Here

For each state like AK,GA..like that 50 states I have to insert
3 names as
greg
jack
jim

Corressponding to each state there should be 3 names..
The other datas are the same.....
Is there anyway I can use like arrays or something like that instead of writing a lot of insert statement

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2004-06-23 : 04:50:45
Something like:-

declare @tblNames table (names varchar(10))
declare @tblStates table (state varchar(2))

insert into @TblNames values ('Jack')
insert into @TblNames values ('Bob')
insert into @TblNames values ('Bill')

insert into @TblStates values ('AA')
insert into @TblStates values ('AB')
insert into @TblStates values ('AC')
insert into @TblStates values ('AD')
insert into @TblStates values ('AE')
insert into @TblStates values ('AF')

select names, state from @TblNames, @TblStates

Then just add your other fields into the select and put an insert into before it...
Go to Top of Page
   

- Advertisement -