Solved

SELECT INSERT INTO OR UPDATE

Posted on 2010-09-22
3
412 Views
Last Modified: 2012-05-10
Hi!

I have a database where i have the stock balance... This i want into a new table and want to update the field or add the field depend if the record exsists....

THE TABLE I WANT TO ADD OR UPDATE DATA TO
SELECT PK, ProdNo, Val1, Val2, Val3 FROM f9000.dbo.FreeInf1

THE TABLE I ASK FROM
SELECT DISTINCT Prod.ProdNo,
      (SELECT SUM(LAGER.Bal + LAGER.stcinc - LAGER.shprsv - LAGER.PicNotR) as Lagersaldohovedlager FROM STCBAL as LAGER WHERE LAGER.Prodno=Prod.ProdNo AND stcNO='1') AS Val1,
      (SELECT SUM(stcbal.InPurc) as IBestilling FROM STCBAL WHERE prod.prodno=stcbal.prodno) AS Val2,
      (SELECT SUM(stcbal.InO) as IOrdre FROM STCBAL WHERE prod.prodno=stcbal.prodno) AS Val3
      --INSERT INTO f9000.dbo.FreeInf1 (ProdNo,Val1,Val2,Val3) VALUES (ProdNo,Prod.LagersaldoHovedlager,Prod.IBestilling,Prod.IOrdre)
FROM F0003.dbo.Prod
WHERE (ProdPrGr='29' AND ProdPrG2='3' AND ProdPrG3='4')
0
Comment
Question by:myhrvold
3 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33733960
what data base product are you using?

unless you are using MySQL, SQL 2008+ or Oracle (possibly others), where you have some "MERGE" syntax, you have to do this with 2 statements.
which would be usually a UPDATE, and if that affected no records, doing a INSERT  ...
0
 
LVL 7

Accepted Solution

by:
mmr159 earned 500 total points
ID: 33734103
If I understand the issue correct, you want to update a record if it exists, otherwise insert a new one... correct?  Here is the syntax that will update or insert, depending on of it exists already.

/*
If record exists in table xyz where x = 99, update y = 77,
otherwise, insert the new record
*/
IF EXISTS (
	SELECT *
	FROM xyz
	WHERE x = 99
	)
BEGIN
	UPDATE xyz SET y = 77
	WHERE x = 99
END
ELSE
BEGIN
	INSERT INTO xyz (x,y,z)
	SELECT 99,77,88
END

Open in new window

0
 

Author Comment

by:myhrvold
ID: 33734146
The database is MS SQL Server 2005
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Subquery Syntax Assistance 13 50
Oracle query output question 4 36
Unable to save view in SSMS 21 59
SQL Query 34 82
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now