Solved

SELECT INSERT INTO OR UPDATE

Posted on 2010-09-22
3
400 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]
Comment Utility
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
Comment Utility
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
Comment Utility
The database is MS SQL Server 2005
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video discusses moving either the default database or any database to a new volume.

771 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

11 Experts available now in Live!

Get 1:1 Help Now