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 |
|
M.E.
Aged Yak Warrior
539 Posts |
Posted - 2002-06-26 : 11:04:46
|
| I can do this with a cursor, but I'm hating cursors more and moreI'll over simplify for thiscreate table Loadingtable(InsertID int)This table is populated with12333223121something along those lines.Create table Destination (InsertID intNewID int)This table is to look likeinsertID,NewID1,11,21,31,42,12,23,13,23,33,43,5The cursor would be something along the lines ofdeclare cursorload first value from LoadingTable into cursorselect @newid = (select count(47) from destination where insertID = @insertID_from_cursor) + 1insert into destination(insertID,NewID)values (@insertID_from_cursor,(@newID)loop cursorkinda sad that I used count(47) hey?Any ideas to make this a set based query?-----------------------Take my advice, I dare ya |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2002-06-26 : 11:10:50
|
| Out of curiosity, what do you need it for? Line numbers on an invoice, or something else? |
 |
|
|
Page47
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2002-06-26 : 11:23:46
|
create table Loadingtable( InsertID int )insert loadingtableselect 1 union all select 2 union all select 3 union all select 3 union all select 3 union all select 2 union all select 2 union all select 3 union all select 1 union all select 2 union all select 1 select insertid, convert(int,'') as newinto #tempfrom loadingtableorder by insertiddeclare @new int, @last intselect @new = 1, @last = 0update #tempset @new = new = case when @last = insertid then @new + 1 else 1 end, @last = insertidselect * from #temp <O> |
 |
|
|
M.E.
Aged Yak Warrior
539 Posts |
Posted - 2002-06-26 : 11:36:10
|
| Ahhh, of course... Temp tables... Now why didn't I think of that. Thanks page, a slight alteration of what you did there will work perfectlyRobIts an issue with keys as much as anything.they have a table full of documents (doc1, doc2, doc3)Coming from a IDMS (from a text file) is a bunch of comments about these documents... Oil well stuff mostly, a new text file full of em comes nightly (something like 3000 per night... It actually has somewhere near 5000 docs and 787000 comments initially). What they want is a table that records what the commentID and a second number to keep track of which document it is (just an arbitrary number assigned by me). So in the future they can reference it by the document ID and this ID I'm assigning it)Its kinda along the lines of Doc1, comment1doc1, comment2doc2, comment1doc2, comment2doc2, comment3...They want a comment number to be assigned to each comment by me as they come into my DB from the flat file.-----------------------Take my advice, I dare ya |
 |
|
|
Page47
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2002-06-26 : 11:39:24
|
| M.E., the important thing there is the order by clause on populating the temp table. Without that, you won't get the expected result. set @var=col=expression is pretty nifty . ..<O> |
 |
|
|
M.E.
Aged Yak Warrior
539 Posts |
Posted - 2002-06-26 : 11:45:56
|
quote: set @var=col=expression
Never really seen it used before actually. Whats that called (besides nifty) and is there an article on sqlteam about it?-----------------------Take my advice, I dare ya |
 |
|
|
Page47
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2002-06-26 : 11:51:14
|
If you look up UPDATE in bol there is some stuff about it.quote: SET @variable = column = expression sets the variable to the same value as the column. This differs from SET @variable = column, column = expression, which sets the variable to the pre-update value of the column.
I picked it up from rrb . . . http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=14095I think here's an article . . . http://www.sqlteam.com/item.asp?ItemID=765<O>Edited by - Page47 on 06/26/2002 11:52:41 |
 |
|
|
M.E.
Aged Yak Warrior
539 Posts |
Posted - 2002-06-26 : 12:09:03
|
| Tis a work of art page.. much thanks-----------------------Take my advice, I dare ya |
 |
|
|
|
|
|
|
|