Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 update and insert in one query

Author  Topic 

CrazyT
Yak Posting Veteran

73 Posts

Posted - 2010-05-04 : 11:53:35
is it possible to combine an update and insert query into one query.

What I'd like to do is insert new data into one table and update a column in a record in the same table at the same time.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-05-04 : 12:17:17
are you using sql 2008?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

ConradK
Posting Yak Master

140 Posts

Posted - 2010-05-04 : 12:20:03
I think its possible, just put the two statements one on top of each other and try it out. It should work.

I do something like:
------------------

create table waits

( sku varchar(64),
weight float,

)
BULK INSERT waits
FROM 'Z:\master weights - 5-03-10.csv'
WITH
( FIRSTROW = 2,
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
alter table waits
add TRUESHIPCOST float
update waits
set trueshipcost = .9+(1.8*waits.weight))
-----------------------------------
on a daily basis. Is something like this what your trying to do?
Go to Top of Page

CrazyT
Yak Posting Veteran

73 Posts

Posted - 2010-05-04 : 12:26:34
using sql 2005
Go to Top of Page

CrazyT
Yak Posting Veteran

73 Posts

Posted - 2010-05-04 : 12:30:14
right now i have it as two statments on top of each other. I was wondering if there was a way to combine them.

basically i have a temp table. it updates row1 column 1 with data from the temp table. based on the data that it is using to update from the temp table i need to insert another record.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-05-04 : 12:32:04
quote:
Originally posted by CrazyT

right now i have it as two statments on top of each other. I was wondering if there was a way to combine them.

basically i have a temp table. it updates row1 column 1 with data from the temp table. based on the data that it is using to update from the temp table i need to insert another record.



have a look at OUTPUT operator to see if it fits your requirement

http://msdn.microsoft.com/en-us/library/ms177564.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -