Solved

Use a select statement to update another table

Posted on 2011-03-22
2
258 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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:Ephraim Wangoya
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

732 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