Author |
Topic |
iori
Starting Member
20 Posts |
Posted - 2008-10-24 : 17:53:35
|
I would like to know whats the best way to do this, say i have one table which has 30+ columns called ORGANIZATION and a related ORGANIZATION_MEMBERS table.ORGANIZATION (ID[AUTO INCREMENTED],info1, info2.....info30)ORGANIZATION_MEMBERS (ORGANIZATION_ID, MemberID)Now i have make a copy each organization in ORGANIZATION table, get the new ID of the organization and make this new organization to have the same members as the old one.I can only think of using cursors and temporary tables (with 30 columns) to do this. Is there a better/faster way of doing this? thinking of fetching those 30 columns in 30 different variables and inserting them using cursors make me lazy! :( |
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2008-10-24 : 18:49:42
|
Let me rephrase...You want to create a new table: NEW_ORGANIZATIONand you want to copy ORGANIZATION and ORGANIZATION_MEMBER information into this new table???Please clarify. |
|
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2008-10-24 : 18:56:02
|
WILL this help:select NEWORGID bigint identity(1,1), O.info1, o,info2, ... info30INTO NEW_ORGANIZATIONfrom ORGANIZATION o, ORGANIZATION_MEMBERS mwhere o.ID = m.ORGANIZATION_ID |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-25 : 02:52:05
|
[code]select identity(int,1,1) AS NewOrganisationID,O.OrgainisationID O.info1, o,info2, ... info30INTO NEW_ORGANIZATIONfrom ORGANIZATION INSERT INTO ORGANIZATION_MEMBERSSELECT no.NewOrganisationID,om.MemberIDFROM ORGANIZATION_MEMBERS omINNER JOIN NEW_ORGANIZATION noON no.Organisation_ID=om.OrganisationID [/code] |
|
|
iori
Starting Member
20 Posts |
Posted - 2008-10-25 : 16:03:54
|
no its not a different ORGANIZATION TABLE, its the same table with new ORGANIZATION id. here is a sample:ORGANIZATION(id, name, location.................)-------------------------------------------------1 organization1 TXand ORGANIZATION_MEMBERS (ORGANIZATION_ID, MemberID) would be------------------------------1 11 21 31 41 51 61 7NOW if i make a new organization based on an exisitng one the tables would look like:ORGANIZATION:----------------------------------1 organization1 TX2 organization2 TX------------------------------1 11 21 31 41 51 61 72 12 22 32 42 52 62 7I am thinking of approaching it with views so the view would bevw_OrgDeails(orgID, orgName,.....OrgMembers)-----------------------------------------------1 organization1 1,2,3,4,5,6,7now using cursors i can loop through it generate a new id and copy members using UDF that will split the string i pass to it and insert it for the new organisation |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-26 : 01:23:52
|
no need of view. you can do thisINSERT INTO ORGANIZATION (name,location,...)select name,location,...from ORGANIZATION INSERT INTO ORGANIZATION_MEMBERSSELECT no1.OrganisationID,om.MemberIDFROM ORGANIZATION_MEMBERS omINNER JOIN ORGANIZATION noON no.Organisation_ID=om.OrganisationIDINNER JOIN ORGANIZATION no1ON no1.Name=no.NameAND no1.Location=no.Location....AND no1.Organisation_ID<> no.Organisation_ID keeping comma seperated values is against normalisation. Also it will make manipulations complex than storing as individual values as you will need UDF to parse the comma seperated list each time. |
|
|
iori
Starting Member
20 Posts |
Posted - 2008-10-26 : 12:49:44
|
thanks visakh16, that should work :) Also I am not saving comma separated values in the table, its a view that i have show user on the UI which is why i was thinking of taking that route. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-26 : 13:55:59
|
quote: Originally posted by iori thanks visakh16, that should work :) Also I am not saving comma separated values in the table, its a view that i have show user on the UI which is why i was thinking of taking that route.
ok. that sounds better. But it might be much easier to do this concatenation for UI in your front end application. Try that way too. |
|
|
|