Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Running a Update query based on information from Insert Qu..

 
   Database Help (Home) -> MSEQ RSS
Next:  Update the MSDE version that comes with office  
Author Message
Wayne

External


Since: Nov 14, 2007
Posts: 6



(Msg. 1) Posted: Wed Jul 23, 2008 10:39 am
Post subject: Running a Update query based on information from Insert Query
Archived from groups: microsoft>public>sqlserver>mseq (more info?)

I am trying to setup a script where I run an insert query to insert
information into a table.
The first part I have down pat as far as the Insert, where I'm having
difficulties is that the information is coming from an outside source and the
input file that goes into the temp table has both new records that need to be
inserted but also might have records that are already in the table that need
to be updated.
What I am trying to accomplish with the code below is that the system will
go in and insert any new records that it finds and then if it finds records
that need updated it would call the update stored procedure to update the
current records in the database based on the information that is in the temp
tables.
I've tried a CASE statement and it didn't work and have tryed a Try/Catch
that didn't work. I had thought just to run the Insert query and then go back
and run the update query but then I end up with 2 records.


INSERT INTO Transportation
(personid, calendarid, inbus, intime, inbusstop, outbus,
outbusstop,milestransported)
SELECT personid, calendarid,inbus, intime, inbusstop, outbus,
outbusstop,milestransported
FROM Transportation1 t1
WHERE NOT EXISTS
(SELECT * FROM Transportation as t
WHERE t.personid = t1.personid AND t.calendarid = t1.calendarid AND t.inbus
= t1.inbus)
GO
EXEC sp_TransportationUpdate
GO

Thanks in Advance for any suggesstions

--
Wayne Hess

 >> Stay informed about: Running a Update query based on information from Insert Qu.. 
Back to top
Login to vote
Hugo Kornelis

External


Since: Jan 11, 2008
Posts: 440



(Msg. 2) Posted: Thu Jul 24, 2008 10:24 pm
Post subject: Re: Running a Update query based on information from Insert Query [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Wed, 23 Jul 2008 10:39:08 -0700, Wayne wrote:

(snip)
> What I am trying to accomplish with the code below is that the system will
>go in and insert any new records that it finds and then if it finds records
>that need updated it would call the update stored procedure to update the
>current records in the database based on the information that is in the temp
>tables.

Hi Wayne,

This is a common problem, and in SQL Server 2008 you can solve this with
the new MERGE statement, that allows you to combine inserting new rows
and updating existing rows in a single statement.

However, in SQL Server 2005 (and before), you still need to execute both
an UPDATE and an INSERT command. Preferably in that order, since you
would otherwise update your just-inserted rows (setting columns to the
same data, so you would not get incorrect results, but you would lose
performance).

The basic pattern is:

UPDATE Target
SET Column1 = Source.Column1,
Column2 = Source.Column2,
...,
ColumnN = Source.ColumnN
FROM Target
INNER JOIN Source
ON Source.PrimaryKey1 = Target.PrimaryKey1
AND Source.PrimaryKey2 = Target.PrimaryKey2;

INSERT INTO Target
(PrimaryKey1, PrimaryKey2,
Column1, Column2, ..., ColumnN)
SELECT PrimaryKey1, PrimaryKey2,
Column1, Column2, ..., ColumnN
FROM Source
WHERE NOT EXISTS
(SELECT *
FROM Target
WHERE Target.PrimaryKey1 = Source.PrimaryKey1
AND Target.PrimaryKey2 = Source.PrimaryKey2);

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis

 >> Stay informed about: Running a Update query based on information from Insert Qu.. 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
update querie multiple tables - Hi I wish to create a stored procedure that updates multiple tables. I have a table called User CREATE TABLE [dbo].[Users]( [UserId] [int] NULL, [Active] [bit] NULL, [OrganisationId] [int] NULL, [Username] [varchar](20), [Password] [varchar](20),....

Simple Query problem - Sample table as follows Order ID Stock Code Status --------- ------- ------- 203 STK1 3 203 STK2 2 203 STK4 3 204 STK1 3 204 STK5 3 205 ..

Hopefully a basic query question - Good afternoon, I am trying to create a query. Underlying data in table is: Job No/Cost Type/$ J8000/1/3000 J8000/2/2000 J8000/2/5000 J8000/2/1000 In the above a Cost type of 1 equates to Labour cost, and a Cost type of 2 equates to Materials cost....

How have you replaced English Query? - Since Microsoft decided to not continue supporting English Query after SQL Server 2000 what have those of you how used it replaced it with?

query assistance -return most recent date - I have a table that has two fields, pkg_num, which is a number, and del_date_time, which is a date-time. The table can contain duplicate pkg_num values, as long as the del_date_time values are different for any given number. I need a query that will...
   Database Help (Home) -> MSEQ All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]