Solved

Use a select statement to update another table

Posted on 2011-03-22
2
257 Views
Last Modified: 2012-08-14
I would like the check_number from the payments table to be updated from the invoice table.

Thanks
SELECT TOP 1  i.invoice_id, i.check_number,ip.ipid, p.check_number
FROM invoice i, invoice_payments ip, payments p
WHERE i.invoice_id = ip.inv_id
AND p.p_id = ip.p_id
AND i.check_number <> 0

UPDATE payments
SET check_number = i.check_number
WHERE p_id = p.p_id

Open in new window

0
Comment
Question by:aka_FATCAT
2 Comments
 
LVL 39

Accepted Solution

by:
appari earned 500 total points
ID: 35195442
try this
UPDATE p
SET check_number = i.check_number
FROM invoice i, invoice_payments ip, payments p
WHERE i.invoice_id = ip.inv_id
AND p.p_id = ip.p_id
AND i.check_number <> 0

Open in new window


note: take backup of your original data or run it in a transaction and after checking the data commit the transaction.
0
 
LVL 32

Expert Comment

by:ewangoya
ID: 35195444
try this
declare @CheckNumber integer, @id integer


SELECT TOP 1 @CheckNumber = i.check_number, @id = p.p_id
FROM invoice i, invoice_payments ip, payments p
WHERE i.invoice_id = ip.inv_id
AND p.p_id = ip.p_id
AND i.check_number <> 0

if (@CheckNumber is not null) and (@CheckNumber > 0)
begin
	UPDATE payments
	SET check_number = @CheckNumber
	WHERE p_id = @id
end

Open in new window

0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

820 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