SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Change Data in column
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

jere1972
Starting Member

2 Posts

Posted - 02/24/2014 :  13:31:19  Show Profile  Reply with Quote
Need some help on how to modify/change data that is in a SQL table column.
Here is what I need or have
I have 2 tabels, Table 1 and Table 2, within those 2 tables there is 1 Column that has the same column heading aswell as data within,(I will call it the serial_number column) there is also a 2nd column that is labeled the Same in both tables, I would like to be able to update the Values in the 2nd column of table 1 with the values from table 2 column 3, using the Serial_number colume as my matching reference between the 2, the data is not in the same order between the 2 Tables,
More or less I need to set the value of table 1/column 2 to match table 2/column 3, where the serial_numbers are the same in both tables serial_number columns, the data being changed is set as Small int...
Any help anyone can provide would be extremely Appreciated, as my working knowledge of Sql Scripting is at a close 0

Edited by - jere1972 on 02/24/2014 13:32:50

TG
Flowing Fount of Yak Knowledge

USA
6062 Posts

Posted - 02/24/2014 :  15:09:19  Show Profile  Reply with Quote
Just JOIN the two tables on serial_number and do the update:
something like this:

update t2 set
       t2.col3 = t1.col2
from   table1 t1
inner join table2 t2
       on t2.serial_number = t1.serial_number


EDIT:
FYI - it is always a good idea to SELECT the data first just as a reality check so you can see what you are about to update.


--update t2 set
--       t2.col3 = t1.col2
SELECT t1.serial_number, t2.col3, t1.col2
from   table1 t1
inner join table2 t2
       on t2.serial_number = t1.serial_number


Another safety check is to first BEGIN TRAN
then check the results - if you don't like it then: ROLLBACK TRAN
if you do like the results then: COMMIT TRAN


Be One with the Optimizer
TG

Edited by - TG on 02/24/2014 15:12:44
Go to Top of Page

maggie21620
Starting Member

USA
27 Posts

Posted - 02/24/2014 :  16:15:16  Show Profile  Reply with Quote
can you help with this, not sure where to put the column or even if that is the right command

--COLUMN PPVTotal FORMAT $99,990
SELECT
adHock.dbo.PPVVINCE$.Corps, adHock.dbo.PPVVINCE$.House, adHock.dbo.PPVVINCE$.Cust,
SUM(PPV_TOTAL) AS PPVTotal

FROM adHock.dbo.PPVVINCE$ INNER JOIN
IDST_74_CUSTOMER ON adHock.dbo.PPVVINCE$.Corps = IDST_74_CUSTOMER.ACCTCORP AND
adHock.dbo.PPVVINCE$.House = IDST_74_CUSTOMER.HOUSE AND adHock.dbo.PPVVINCE$.Cust = IDST_74_CUSTOMER.CUST





GROUP BY adHock.dbo.PPVVINCE$.Corps, adHock.dbo.PPVVINCE$.House, adHock.dbo.PPVVINCE$.Cust
ORDER BY PPVTota

none
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 02/27/2014 :  02:54:01  Show Profile  Reply with Quote
quote:
Originally posted by maggie21620

can you help with this, not sure where to put the column or even if that is the right command

--COLUMN PPVTotal FORMAT $99,990
SELECT
adHock.dbo.PPVVINCE$.Corps, adHock.dbo.PPVVINCE$.House, adHock.dbo.PPVVINCE$.Cust,
SUM(PPV_TOTAL) AS PPVTotal

FROM adHock.dbo.PPVVINCE$ INNER JOIN
IDST_74_CUSTOMER ON adHock.dbo.PPVVINCE$.Corps = IDST_74_CUSTOMER.ACCTCORP AND
adHock.dbo.PPVVINCE$.House = IDST_74_CUSTOMER.HOUSE AND adHock.dbo.PPVVINCE$.Cust = IDST_74_CUSTOMER.CUST





GROUP BY adHock.dbo.PPVVINCE$.Corps, adHock.dbo.PPVVINCE$.House, adHock.dbo.PPVVINCE$.Cust
ORDER BY PPVTota

none


this looks different from your initial explanation
So which is the field you want to compare on? I see more than 1 fields in comparison here
ALso which field need to update? Why are you using GROUP BY?? I cant see anything whch speaks about aggregation in your explanation.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000