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 2005 Forums
 Transact-SQL (2005)
 Loop on Each Record

Author  Topic 

kasaya
Starting Member

10 Posts

Posted - 2009-02-17 : 01:26:01
Hi,
Can u Help me in this situation....
for example :
i have 10 records and i have to use each one of them in to update another table...
record 1 will update record 1 in other table
record 2 will update record 2 in other table.
and so on..,
what method should i use
in programming this situation will look like this
for each row in record row
update set datarow = row..

hope u understand my situation ^__^
arigato....

Nageswar9
Aged Yak Warrior

600 Posts

Posted - 2009-02-17 : 01:34:01
update tabl1
set col1 = values-----------
from tabl1 t1
inner join tabl2 t2 on t2.id = t3.id
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-02-17 : 01:43:18
u can create a stored procedures and then update the fields by passing parameters
Go to Top of Page

kasaya
Starting Member

10 Posts

Posted - 2009-02-17 : 01:53:20
select distinct user from tblUsers...
-----and for example the output will look like this
user1
user2
user3
user4

for each output the first query gives there will be an insert statement like this
insert into tblExistingUser user1
insert into tblExistingUser user2
insert into tblExistingUser user3
insert into tblExistingUser user4

the above statements should look like...
i cant explain clearly my situation hope you can understand it ^__^
arigato...
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-02-17 : 01:57:10
create proc usp_samp
(
@user varchar(32)
)
set nocount on

insert into tblexistinguser (usernamecol)
select @user
--if any cond..

set nocount off
Go to Top of Page

kasaya
Starting Member

10 Posts

Posted - 2009-02-17 : 01:57:27
quote:
Originally posted by bklr

u can create a stored procedures and then update the fields by passing parameters


hi,
yes that is what i am trying to do..
first i will query the distinct value
then after that i want to loop in each of the query result and insert it using each of the query result..

arigato
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-02-17 : 01:59:34
u will get the datatable for select statement
from front end pass the each username
in a while loop and call the sp then u can insert the values in to existinguser tables
Go to Top of Page

kasaya
Starting Member

10 Posts

Posted - 2009-02-17 : 02:01:54
quote:
Originally posted by bklr

u will get the datatable for select statement
from front end pass the each username
in a while loop and call the sp then u can insert the values in to existinguser tables



hi,
is there a way to do this in the SP's
im trying to avoid many query inside my form to avoid LAG's
arigato
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-02-17 : 02:08:03
u want to update the values in the tblexisting user
r insert the record in to tblexistinguser

then directly insert into tblexistinguser (usernamecol)
select username from tblusers
Go to Top of Page

kasaya
Starting Member

10 Posts

Posted - 2009-02-17 : 02:12:34
quote:
Originally posted by bklr

u want to update the values in the tblexisting user
r insert the record in to tblexistinguser

then directly insert into tblexistinguser (usernamecol)
select username from tblusers


hi,
i want to insert the user in tblUser into tblExistingUser
yes...that's... can you help me on how to do this

arigato
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-02-17 : 02:16:10
try like this
insert into tblexistinguser (usernamecol)
select distinct username from tblusers
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-17 : 03:00:41
or did you mean this?

insert into tblexistinguser (usernamecol)
select distinct t.username from tblusers t
where not exists (select 1 from tblexistinguser where usernamecol= t.username)
Go to Top of Page

kasaya
Starting Member

10 Posts

Posted - 2009-02-19 : 01:49:29
quote:
Originally posted by visakh16

or did you mean this?

insert into tblexistinguser (usernamecol)
select distinct t.username from tblusers t
where not exists (select 1 from tblexistinguser where usernamecol= t.username)




hi,
thanks this code is very complex in a way that it have many select statements but it works. :D ^_^
i used fetch but this code is way better...
thanks a lot
-kasaya
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-19 : 03:43:21
welcome
Go to Top of Page
   

- Advertisement -