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
 update and insert

Author  Topic 

barflyz
Starting Member

47 Posts

Posted - 2010-05-14 : 13:52:22
I have a table (table1) with a bunch of records say 50.

this table has a code that is repeated in one column for the 50 records, example code is 123456

I want to update/change code 123456 to a new code based on a list of distinct codes stored in a second table (table2) but evaluate and update one at a time, example below

example: the first distinct code from table2 is code 456789, I want to take that code and update code 123456 in table1 to 456789 in table1 and then append these records from table1 to a 3rd table (table3)

then repeat the process for each distinct code in table2,

next code
the second distinct code from table2 is code 666666, I want to take that code and update code 456789 in table1 to 666666 in table1 and then append these records from table1 to a 3rd table (table3)

and repeat, repeat, repeat


I know you have to use a variable and loop but not that sure how to accomplish.

thanks

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-05-14 : 14:53:06
quote:
and repeat, repeat, repeat

This is not the way to go...we need to look at a set-based solution.
quote:
I want to update/change code 123456 to a new code based on a list of distinct codes stored in a second table (table2) but evaluate and update one at a time

This means table1 will have the last value of the distinct code form table2...I don't see the need to update table1?
quote:
append these records from table1 to a 3rd table (table3)

So ideally what you want is all rows in table1 repeated that many number of times as the number of distinct codes you have? Is that right.

So..consider this example.
Table1 Data
declare @table1 table (col1 int,col2 int,code int)
insert @table1
select 1,1,123456
union all select 1,2,123456
union all select 1,3,123456
union all select 2,1,123456
union all select 2,2,123456
union all select 2,3,123456

Table2 Data
declare @table2 table (code int)
insert @table2
select 456789
union all select 666666
union all select 888888
union all select 456789


So when you run this...
select a.col1,a.col2,b.code
from @table1 a cross apply (select distinct code from @table2) b
order by b.code

you get this
col1        col2        code
----------- ----------- -----------
1 1 456789
1 2 456789
1 3 456789
2 1 456789
2 2 456789
2 3 456789
1 1 666666
1 2 666666
1 3 666666
2 1 666666
2 2 666666
2 3 666666
1 1 888888
1 2 888888
1 3 888888
2 1 888888
2 2 888888
2 3 888888

Is this the data you want inserted into table3?
If not, please explain with sample data and expected output.
Go to Top of Page

barflyz
Starting Member

47 Posts

Posted - 2010-05-14 : 15:04:58
"So ideally what you want is all rows in table1 repeated that many number of times as the number of distinct codes you have? Is that right."

yes, but the codes have to be changed to the distinct codes also from the list

I believe we are on the same page but I am just not sure how to code it.

so table1 data ends up getting repeated like 2k+ times (2k+ distinct codes in table 2) * 50 rows and inserted into table 3 but the codes have to be changed for each block of 50 to each distinct chosen. I did something like this with looping and variables with unix and neteeza like 5 years ago but I am having trouble remembering thanks!
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-05-14 : 15:10:59
Did you see my sample data?
Table1 has 6 rows..and Table 2 has 3 distinct codes....when I run the select (also shown)...the 6 rows are repeated 3 times (once for each distinct code) ..SO you have 18 rows in total in the final result set.
This is whatyou want inserted into Table3?
Go to Top of Page

barflyz
Starting Member

47 Posts

Posted - 2010-05-14 : 15:48:44
yes I believe so
Go to Top of Page

barflyz
Starting Member

47 Posts

Posted - 2010-05-14 : 16:20:08
if this is the code I am not sure I follow -
Go to Top of Page
   

- Advertisement -