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 |
IKU
Starting Member
11 Posts |
Posted - 2009-07-08 : 16:16:24
|
Here is my problem:I'm trying to insert data into a table which has primary key of 3 columns. Data that I'm trying to insert only have two columns, so that third column should be a kind of identity, but it would have to reset on change of first two columns. Example:This is the current state of datacol1 col2 ---------- ----- 22-15-09 000322-15-09 000322-15-09 000322-15-09 000522-15-09 000622-15-09 000622-15-09 000622-15-09 000622-15-09 000622-15-09 0006result should be:col1 col2 ---------- ----- col322-15-09 0003 122-15-09 0003 222-15-09 0003 322-15-09 0005 122-15-09 0006 222-15-09 0006 322-15-09 0006 422-15-09 0006 522-15-09 0006 622-15-09 0006 7 |
|
IKU
Starting Member
11 Posts |
Posted - 2009-07-08 : 17:27:26
|
I've made an error;result should be:col1 col2 ---------- ----- col322-15-09 0003 122-15-09 0003 222-15-09 0003 322-15-09 0005 122-15-09 0006 122-15-09 0006 222-15-09 0006 322-15-09 0006 422-15-09 0006 522-15-09 0006 6sorry |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-07-09 : 01:51:31
|
[code]if u dont have any identity column then try thisdeclare @tab table (col1 varchar(32), col2 varchar(32))insert into @tab select '22-15-09', '0003' union all select '22-15-09', '0003' union all select '22-15-09', '0003' union all select '22-15-09', '0005' union all select '22-15-09', '0006' union all select '22-15-09', '0006' union all select '22-15-09', '0006' union all select '22-15-09', '0006' union all select '22-15-09', '0006' union all select '22-15-09', '0006'drop table #tempselect identity(int,1,1)as id , * into #temp from @tabselect *,(select count(1) from #temp where col1 = t.col1 and col2 = t.col2 and id <=t.id)as col3from #temp t[/code]if u have any identity column then dont use #temp table directly use ur table and inplace of id use identity column |
|
|
IKU
Starting Member
11 Posts |
Posted - 2009-07-09 : 02:28:27
|
Thank you, it seems to be working |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-07-10 : 01:16:00
|
welcome |
|
|
|
|
|
|
|