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]

 

Author table design
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?


shiv_koirala@yahoo.com

2005-10-29, 3:23 am

Its 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/

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)
Sponsored Links





Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive | Programming forum archive

Copyright 2008 droptable.com