Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Updating a Live Table using a New Table

Status
Not open for further replies.

codecontractor

Programmer
Jan 8, 2005
13
US
Ok, first of all, I would like to thank you all for your guidance in the past. I am new to SQL and SQL Server, but with the help of this forum I have picked it up pretty quickly.

So, my situation is:
I have a live (actively used) table which is full text indexed. I have another table that is actually a new version of that table, but it is not full text indexed. I am looking to update the LIVE table from the NEW table with a stored procedure/function. I would just rename the two, but then I lose the full text indexing.The tables have the same column names and the ID field is what I use to find records to compare. See my code below...

Code:
ALTER PROCEDURE UpdateLive
-- This code finds records where the Title
-- field has changed in NEW and adjusts LIVE
AS
UPDATE	live
SET	live.Title = new.Title
FROM 
	NewTable as new INNER JOIN LiveTable as live
	ON 
	(
		live.ID = new.ID and
		live.Title != new.Title
	)
This just works for the one field, but I have 40 fields. Is there is a way to do this for multiple fields with one query like this?

Thanks,
-Ben
 
ALTER PROCEDURE UpdateLive
-- This code finds records where the Title
-- field has changed in NEW and adjusts LIVE
AS
UPDATE live
SET live.Title = new.Title,
live.Field2 = New.Field2,
Live.Field3 = New.Field3,
Live.etc = New.Etc
FROM
NewTable as new INNER JOIN LiveTable as live
ON
(
live.ID = new.ID and
live.Title != new.Title
)
 
Wow, thanks. That works perfectly. This is how I have changed it:
Code:
ALTER PROCEDURE UpdateLive
-- This code finds records where the Title
-- field has changed in NEW and adjusts LIVE
AS
UPDATE    live
SET    live.Title = new.Title,
       live.Field2 = New.Field2,
       Live.Field3 = New.Field3
       ------ ....
FROM
    NewTable as new INNER JOIN LiveTable as live
    ON
    (
        live.ID = new.ID and (
        live.Title != new.Title OR
        live.field2 != new.field2 OR
        live.field2 != new.field3 
        ------ .... )
    )
Still, it seems like there should be a more generic way of doing this... what if I had 300 columns?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top