| Author |
Topic |
|
elwoos
Master Smack Fu Yak Hacker
2052 Posts |
Posted - 2004-09-07 : 12:16:44
|
| I'm trying to create some testing data in a script.At the moment I create 10 people each with an ID that is generated by the system and store those ID's in a temporary table. I also have another table of contacts which indicate that contact has been made with a person. Again these have a system generated ID and are stored in a temporary table. What I want to do is tie these together so that each person has one and only one different contact.e.g. I have a table called #TEMPID:-ID15739...and a table called #TEMPCONTACTIDContactID 2345674321...What I want is a statement that will create something likeContactID ID 23 945 367 143 521 7...Note that one of the columns should ideally be randomly ordered as otherwise these could both be consecutive series which is not really ideal. Is it possible to do this? I presume that I need some extra data in at least one of my temporary tables but what? Do I need a cross join?Many thankssteveSteve no function beer well without |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2004-09-07 : 12:27:35
|
| what you need is a relationship between your two tables, of which you've shown none. How do you propose to relate TempID to TempContactID ?- Jeff |
 |
|
|
elwoos
Master Smack Fu Yak Hacker
2052 Posts |
Posted - 2004-09-07 : 12:43:09
|
| I'd like them to be joined randomly which is why I was wondering about a Cross Join. I can add anything I want to these temporary tables as long as I get the final resultthankssteveSteve no function beer well without |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2004-09-07 : 12:44:38
|
quote: Originally posted by elwoos Steve no function beer well without
Start drinking....OK, ahem...huh?USE NorthwindGOSET NOCOUNT ONCREATE TABLE #TEMPID([ID] int)GOINSERT INTO #TEMPID([ID])SELECT 1 UNION ALLSELECT 5 UNION ALLSELECT 7 UNION ALLSELECT 3 UNION ALLSELECT 9GOCREATE TABLE #TEMPCONTACTID (ContactID int)GOINSERT INTO #TEMPCONTACTID(ContactID) SELECT 23 UNION ALLSELECT 45 UNION ALLSELECT 67 UNION ALLSELECT 43 UNION ALLSELECT 21GOCREATE TABLE myTable99([ID] int, ContactID int)DECLARE @ID int, @ContactID intDECLARE myCursor99 CURSOR FOR SELECT [ID] FROM #TEMPID ORDER BY NEWID()DECLARE myCursor00 CURSOR FOR SELECT ContactID FROM #TEMPCONTACTIDOPEN myCursor99OPEN myCursor00FETCH NEXT FROM myCursor99 INTO @ID FETCH NEXT FROM myCursor00 INTO @ContactIDWHILE @@FETCH_STATUS = 0 BEGIN INSERT INTO myTable99([ID], [ContactID]) SELECT @ID, @ContactID FETCH NEXT FROM myCursor99 INTO @ID FETCH NEXT FROM myCursor00 INTO @ContactID ENDCLOSE myCursor99CLOSE myCursor00DEALLOCATE myCursor99DEALLOCATE myCursor00SELECT * FROM myTable99GOSET NOCOUNT OFFDROP TABLE #TEMPIDDROP TABLE #TEMPCONTACTIDDROP TABLE myTable99GO Brett8-) |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2004-09-07 : 12:50:24
|
could it possibly be that this is a task that can be only done with cursors or a while, as in not set based???Go with the flow & have fun! Else fight the flow |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2004-09-07 : 13:10:08
|
| could it be that this is a task that doesn't make any sense?Steve...do you know which rows are supposed to match up with which?If so, how?Brett8-) |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2004-09-07 : 13:54:58
|
acctually this kind of makes sense to me...Employees get random tasks... it's like a game "pick a card, any card from Deck" Go with the flow & have fun! Else fight the flow |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2004-09-07 : 14:20:22
|
| 1. insert the first list into a temp table, with an identity:select ID, IDENTITY(1,1) as Positioninto #Temp1from IDList2. insert the second list into a temp table, also with an identity:select ContactID, IDENTITY(1,1) as Positioninto #Temp2from ContactIDList3. Now we can relate ContactID's to ID's using the Position column:select T1.ID, T2.ContactIDfrom #Temp1 T1inner join #Temp2 T2on T1.Position = T2.Positionand do what you need from there. Add ORDER BY NEWID() to either step 1 and/or 2 to add some randomness to this if needed.- Jeff |
 |
|
|
elwoos
Master Smack Fu Yak Hacker
2052 Posts |
Posted - 2004-09-08 : 11:08:28
|
| Thanks guys I'll take a look at those. In response to your question Brett, Spirit is right. It's rather like having five people in a room and five cards where they have to pick one each.Hope this is clearersteveSteve no function beer well without |
 |
|
|
elwoos
Master Smack Fu Yak Hacker
2052 Posts |
Posted - 2004-09-08 : 11:11:41
|
| Actually I've just realised that the example I gave wasn't quite right in that the #TEMPID table and the #TEMPCONTACTID table are likely to be sorted numerically, though not neccessarily consecutive numbers. i.e. #TEMPID should have beenID1357910 (added to show that it's not just odd numbers)...steveSteve no function beer well without |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2004-09-08 : 11:45:58
|
| did you try/follow my idea? it will work for you.- Jeff |
 |
|
|
elwoos
Master Smack Fu Yak Hacker
2052 Posts |
Posted - 2004-09-09 : 03:59:49
|
| Jeff, yes it works, thankssteveSteve no function beer well without |
 |
|
|
|