simple question on inserts and updates

simple question on inserts and updates

Post by Kevin S. Gof » Sun, 10 Mar 2002 14:34:54



Maybe this is a stupid question, but...

Let's say I have a tempoary table with four records that a user has
chosen...in this case, products on a bill of material.  I want to
update the actual production file.  In that production file, two of
the four records may already exist, and two don't...or maybe all four
exist, or maybe one exists but three don't.

I need to either insert or add all four.  How can I write one line in
T-SQL to do so?

On a straight insert where I know that the four user entries don't
exist, I do a...

INSERT productionfile FROM SELECT <fields> FROM #productionfile

And if the four already exist, I do a...

UPDATE productionfile FROM #productionfile WHERE <match the product
IDs>

But how can I write one line that would update all fields from the
temp table where product keys do exist, and inserts where they don't?

Thanks,
Kevin

 
 
 

simple question on inserts and updates

Post by Steve Kas » Sun, 10 Mar 2002 14:44:54


Kevin,

  You can update, then insert:

update productionfile from #productionfile
set ...
where #productionfile.prodID = productionfile.prodID

insert into productionfile
select * from #productionfile
where not exists (
  select * from productionfile
  where #productionfile.prodID = productionfile.prodID
)

Steve Kass
Drew University


> Maybe this is a stupid question, but...

> Let's say I have a tempoary table with four records that a user has
> chosen...in this case, products on a bill of material.  I want to
> update the actual production file.  In that production file, two of
> the four records may already exist, and two don't...or maybe all four
> exist, or maybe one exists but three don't.

> I need to either insert or add all four.  How can I write one line in
> T-SQL to do so?

> On a straight insert where I know that the four user entries don't
> exist, I do a...

> INSERT productionfile FROM SELECT <fields> FROM #productionfile

> And if the four already exist, I do a...

> UPDATE productionfile FROM #productionfile WHERE <match the product
> IDs>

> But how can I write one line that would update all fields from the
> temp table where product keys do exist, and inserts where they don't?

> Thanks,
> Kevin


 
 
 

simple question on inserts and updates

Post by Umachandar Jayachandra » Sun, 10 Mar 2002 14:45:24


Quote:>> But how can I write one line that would update all fields from the
>> temp table where product keys do exist, and inserts where they don't

    You cannot do that in a single statement. You have to do insert/update
one after another like:

begin tran
insert into productionfile
select ...
  from #productionfile as p1
 where not exists( select * from productionfile as p2
                    where p2.prodid = p1.prodid )

update productionfile
   set ...
  from #productionfile as p1
 where p1.prodid = productionfile.prodid
commit

--
Umachandar Jayachandran
SQL Resources at http://www.umachandar.com/resources.htm
( Please reply only to newsgroup. )

 
 
 

1. Simple XML Update question - Update a tag.

I have the following xml file and I need to update the RefID tag... does
anyone know how to do this using javascript?  I need to change the text in
this tag before I acutally submit it....  seems like a simple thing to do
but its causing me large problems.

<?xml version="1.0" ?>
  <!DOCTYPE XML_PriceAvailability_Submit SYSTEM
"XML_PriceAvailability_Submit.dtd">
  <XML_PriceAvailability_Submit>
    <Detail>
      <LineInfo>
        <AssignedID>001</AssignedID>
        <RefIDQual>VP</RefIDQual>
        <RefID>267851</RefID>
      </LineInfo>
     </Detail>
    <Summary>
      <NbrOfSegments>2</NbrOfSegments>
    </Summary>
  </XML_PriceAvailability_Submit>

Thanks in advance!

Erik Klein

2. Filemaker crashes when opening script

3. how to write a simple trigger for update and insert

4. FoxPro programmer wanted

5. SQL7, SIMPLE SIMPLE SIMPLE question

6. Appl. Software Promax on Informix

7. Newbie: Looking for Simplest Code to OpenConnection, OpenRecordset, Execute A SQL Statement Insert/Update

8. please help! ssql23.zip from simtel20.army.mil

9. can't do a simple sql update statement (but insert works ok)

10. Create Trigger - Insert, Update - not updating record on Insert

11. Waht is better: Insert and update or Select Update else insert

12. ODBC - insert/updating two databases with one insert/update