Solved

Use a select statement to update another table

Posted on 2011-03-22
2
254 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

758 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

20 Experts available now in Live!

Get 1:1 Help Now