Drop Table
Support Forum for database administrators and web based access to important newsgroups related to databasesI am a novice with SQL, so please don't hold it against me. I have a text file that I import into SQL. (easy enough) There are not many fields (maybe 20) THere are is One field named PO which can be in multiple times. Another field name STYLE that can also have duplicate rcords. BUT The same PO number with the same STYLE number will not duplicate. But I will have another text file to import that would update info for the record that has the same PO and Style. How would I do that? ANy place on teh Internet where I might be able to figure this out?
Post Follow-up to this messageIts a one to many relationship for PO and STYLE. ------- Regards , C#, VB.NET , SQL SERVER , UML , DESIGN Patterns Interview question book http://www.geocities.com/dotnetinterviews/ My Interview Blog http://spaces.msn.com/members/dotnetinterviews/
Post Follow-up to this messageOn Fri, 28 Oct 2005 13:37:11 -0700, Johnfli wrote: >I am a novice with SQL, so please don't hold it against me. > >I have a text file that I import into SQL. (easy enough) >There are not many fields (maybe 20) >THere are is One field named PO which can be in multiple times. Another >field name STYLE that can also have duplicate rcords. BUT The same PO >number with the same STYLE number will not duplicate. >But I will have another text file to import that would update info for the >record that has the same PO and Style. How would I do that? >ANy place on teh Internet where I might be able to figure this out? > Hi Johnfli, Import your second text file to a staging table. Then merge it onto the main table with the following two commands: UPDATE MainTable SET Column1 = StagingTable.Column1, Column2 = StagingTable.Column2, -- repeat for all columns except PO and STYLE FROM MainTable JOIN StagingTable ON StagingTable.PO = MainTable.PO AND StagingTable.STYLE = MainTable.STYLE INSERT INTO MainTable (PO, STYLE, Column1, Column2, ...) SELECT PO, STYLE, Column1, Column2, ... FROM StagingTable WHERE NOT EXISTS (SELECT * FROM MainTable WHERE MainTable.PO = StagingTable.PO AND MainTable.STYLE = StagingTable.STYLE) Best, Hugo -- (Remove _NO_ and _SPAM_ to get my e-mail address)
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread