|
Home > Archive > MS SQL Server > October 2005 > table design
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Johnfli 2005-10-28, 8:23 pm |
| 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?
| |
|
|
| Hugo Kornelis 2005-10-29, 8:23 pm |
| On 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)
|
|
|
|
|