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
 General SQL Server Forums
 New to SQL Server Programming
 Loop in result to an uopdate qry

Author  Topic 

barflyz
Starting Member

47 Posts

Posted - 2010-05-14 : 10:59:27
Hi, if we have a list of distinct values from a table that we want to loop into another query as the "where" clause how can this be accomplished?

example:

result of a distinct select gives me values,

111,
213
345
456 etc. (over 2k in results)

My insert qry example:

insert into table1
as select *
from table 2 where code = '213'

after this runs, runs again with the next code (shown below) as follows until it reaches the end of the codes returned from the distinct qry above

insert into table1
as select *
from table 2 where code = '111'

thanks

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-05-14 : 11:01:04
[code]insert into table1
as select *
from table 2 where code in (<insert query to get the distinct select>)[/code]
Go to Top of Page

barflyz
Starting Member

47 Posts

Posted - 2010-05-14 : 11:02:25
I have to run each one from the where clause one at a time for each code if that makes sense.
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-05-14 : 11:04:21
quote:
Originally posted by barflyz

I have to run each one from the where clause one at a time for each code if that makes sense.



Why? The query will insert all codes at once. You dont want that?
Go to Top of Page

barflyz
Starting Member

47 Posts

Posted - 2010-05-14 : 11:05:43
different insert data based on each code code selected
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-05-14 : 11:09:41
can you be more specific on what the insert should really do?


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

barflyz
Starting Member

47 Posts

Posted - 2010-05-14 : 11:27:37
sure, we have a table named table1 and I want to say when this table 1 id = 20344 insert all values when this table1's id = 20343 and then I want to repeat again as follows (table on table , it is a transactional system so we need to add block and blocks of data)

when table 1 id = 20345 (changed to 20345 by select distinct id from table1) insert all values only when table1's id = 20343 (this stays the same as I know it has all values to insert)

so I want all records from table1 when id is 20343 to insert into table 1 when id is 20344, 20345, 20346 (I have to loop in these id codes based on values in table, of course other than 20343 since that is the driver)
Go to Top of Page

barflyz
Starting Member

47 Posts

Posted - 2010-05-14 : 11:30:01
the post from chrisbucci8 is a co-worker also looking for the same answer, maybe his is clearer than mine?
thanks!!!
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-05-14 : 11:34:02
No it is not clearer to me.
But if this is the same thing then the answer should be placed there.
It is not a good idea to have two threads with the same problem.
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=144622


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -