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.
Author |
Topic |
mayoorsubbu
Yak Posting Veteran
95 Posts |
Posted - 2014-01-21 : 06:43:50
|
Dear All,I have a table tbItem in SQL Server 2000 as below.Datatype intPrimaryItem SecondaryItem1111 987661111 654381111 453242222 513322222 87656I want to insert data into the same table with different combinations of primaryItem and SecondaryItem like underPrimaryItem SecondaryItem98766 111198766 6543898766 4532465438 111165438 98766 65438 4532445324 1111 45324 98766 45324 65438 51332 222251332 8765687656 222287656 51332Can I do it use set based operations or is a cursor based approach required. How do I achieve it? Thank You |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2014-01-21 : 07:43:14
|
[code]insert into tbItem ( PrimaryItem , SecondaryItem )select PrimaryItem , SecondaryItem from( select distinct PrimaryItem from tbItem ) pcross join( select distinct SecondaryItem from tbItem ) swhere not exists (select * from tbItem x where x.PrimaryItem = p.PrimaryItem and x.SecondaryItem = s.SecondaryItem)[/code] KH[spoiler]Time is always against us[/spoiler] |
|
|
mayoorsubbu
Yak Posting Veteran
95 Posts |
Posted - 2014-01-21 : 08:17:39
|
Thank you khtan for the help. |
|
|
mayoorsubbu
Yak Posting Veteran
95 Posts |
Posted - 2014-01-22 : 04:49:02
|
khtan, I m sorry but this does not produce the desired result. |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2014-01-22 : 06:34:01
|
[code]insert into tbItem ( PrimaryItem , SecondaryItem )select PrimaryItem , SecondaryItem from( select distinct PrimaryItem from tbItem union select distinct SecondaryItem from tbItem ) pcross join( select distinct SecondaryItem from tbItem union select distinct PrimaryItem from tbItem ) swhere not exists (select * from tbItem x where x.PrimaryItem = p.PrimaryItem and x.SecondaryItem = s.SecondaryItem)[/code] KH[spoiler]Time is always against us[/spoiler] |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2014-01-22 : 08:42:26
|
quote: Originally posted by mayoorsubbu Dear All,I have a table tbItem in SQL Server 2000 as below.Datatype intPrimaryItem SecondaryItem1111 987661111 654381111 453242222 513322222 87656I want to insert data into the same table with different combinations of primaryItem and SecondaryItem like underPrimaryItem SecondaryItem98766 111198766 6543898766 4532465438 111165438 98766 65438 4532445324 1111 45324 98766 45324 65438 51332 222251332 8765687656 222287656 51332Can I do it use set based operations or is a cursor based approach required. How do I achieve it? Thank You
INSERT INTO TableName(PrimaryItem,SecondaryItem)SELECT SecondaryItem,PrimaryItemFROM TableNameUNION ALLSELECT t1.SecondaryItem,t2.SecondaryItemFROM TableName t1INNER JOIN TableName t2ON t2.PrimaryItem = t1.PrimaryItemWHERE t1.SecondaryItem <> t2.SecondaryItem ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
mayoorsubbu
Yak Posting Veteran
95 Posts |
Posted - 2014-01-23 : 09:07:54
|
Once again thank u visakh and khtan. Will try and revert in case of any issue. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2014-01-23 : 13:19:51
|
coollet us know how you got on!------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|
|
|
|
|