Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
oracle765
Starting Member
13 Posts |
Posted - 2013-02-22 : 00:26:00
|
Hi allI am using sqlserver 2008Im new to sqlserver and I need to do an update statement for my boss, I have worked all week but still cannot figure it outll i am trying to do is update table1 column1's value if table2 columns 1 values match with table2 columns2 value bare in mind there are thousands of rowsexampletable 1 contains:column 1:microsoft outlook 2007,microsoft 2007 outlook,microsoft out look 2007,microsoft 2007 office,microsoft office 2007 plus lots moretable 2 contains:column 1:microsoft outlook 2007,microsoft 2007 outlook,microsoft out look 2007,microsoft 2007 office,microsoft office 2007 plus lots moreand in column 2:outlook 2007,office 2007 plus lots moreso the final result should be!: in table 1, column 1 should sayoutlook 2007office 2007 which is table 2s expected valuethe query i have is as follows i do not know if this is the right way in thinking------------------------------------------------------- with C as(select distinct RTRIM(LTRIM(TGT.software_name_raw)) as Target_Name,RTRIM(LTRIM(SRC.software_name_raw)) as Source_Namefrom dbo.BigTable as TGTINNER JOIN dbo.RefTable as SRCon TGT.software_name_raw = SRC.software_name_raw)update Cset Target_Name = Source_Name--------------------------------------------------------it is also producing an error sayingMsg 4406, Level 16, State 1, Line 14Update or insert of view or function 'C' failed because it contains a derived or constant field.Please helpA Lynch |
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2013-02-22 : 03:41:38
|
I'm a little confused about the requirements but I think this is actually really simple. Mind you that the value for the join condition (on TGT.software_name_raw = SRC.software_name_raw) must be equal in both tables for the join to work properlyupdate TGT set TGT.column_with_wrong_value = RTRIM(LTRIM(SRC.column_with_correct_value))from dbo.BigTable as TGT INNER JOIN dbo.RefTable as SRC on TGT.software_name_raw = SRC.software_name_raw - LumbagoMy blog-> http://thefirstsql.com |
|
|
oracle765
Starting Member
13 Posts |
Posted - 2013-02-22 : 05:22:20
|
Thanks lumbago firstly thanks for your prompt response I have noticed one error in that I am not trying to update with the software name amended when i should besecondly I hope this can explain it furtherif table 1's software_name_raw = table2's software_name_raw then I want to update table 1s rows with table 2's software_name_amended value of what it should be.for example the 4th row should change to Visio 2003 Viewer the 6th row should change to Visio 2007if there is no match then just leave table1's software_name_raw value as it isI hope this helps you guys as I am now starting to confuse myself further lolTABLE1-COL1 called software_name_rawMicrosoft Office Visio Viewer 2003 (English)Microsoft Office Visio 2003 Step by StepMicrosoft Office Visio 2003 Step by StepMicrosoft Office Visio Viewer 2003 (English)Microsoft Office Visio Viewer 2003 (English)Microsoft Office Visio 2007 (Exe)Microsoft® Office Visio® 2007Microsoft® Office Visio® 2007Microsoft Office Visio 2007 Professional EditionMicrosoft Office Visio Professional 2007Microsoft Office Visio Viewer 2007Microsoft Office Visio Viewer 2007Microsoft Office Visio Viewer 2007Microsoft Office Visio Viewer 2007Microsoft Office Visio 2010Microsoft Office Visio 2010Microsoft Office Visio 2010 (Exe)Microsoft Office Visio 2010 Premium EditionMicrosoft Office Visio 2010 Professional EditionTABLE2-COL1 also called software_name_raw TABLE2-COL2 called software_name_ammendedMicrosoft Office Visio Viewer 2003 (English), Visio 2003 ViewerMicrosoft Office Visio 2003 Step by Step, Visio 2003 Step by StepMicrosoft Office Visio 2003 Step by Step, Visio 2003 Step by StepMicrosoft Office Visio Viewer 2003 (English), Visio 2003 ViewerMicrosoft Office Visio Viewer 2003 (English), Visio 2003 ViewerMicrosoft Office Visio 2007 (Exe), Visio 2007Microsoft® Office Visio® 2007, Visio 2007Microsoft® Office Visio® 2007, Visio 2007Microsoft Office Visio 2007 Professional Edition, Visio 2007 ProfessionalMicrosoft Office Visio Professional 2007, Visio 2007 ProfessionalMicrosoft Office Visio Viewer 2007, Visio 2007 ViewerMicrosoft Office Visio Viewer 2007, Visio 2007 ViewerMicrosoft Office Visio Viewer 2007, Visio 2007 ViewerMicrosoft Office Visio Viewer 2007, Visio 2007 ViewerMicrosoft Office Visio 2010, Visio 2010Microsoft Office Visio 2010, Visio 2010Microsoft Office Visio 2010 (Exe), Visio 2010Microsoft Office Visio 2010 Premium Edition, Visio 2010 PremiumMicrosoft Office Visio 2010 Professional Edition, Visio 2010 Professional A Lynch |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2013-02-22 : 06:58:38
|
Try the code that Lumbago gave you. Sure looks like it should work.If it does not, follow the link in my signature, and it will show you how to give us DDL and sample data so that we can help you better.How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx |
|
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2013-02-22 : 07:44:23
|
Do you have a SoftwareID-column or something in these tables?- LumbagoMy blog-> http://thefirstsql.com |
|
|
oracle765
Starting Member
13 Posts |
Posted - 2013-02-22 : 15:49:59
|
No I don't would it be better if so how would I do this to ref each tableA Lynch |
|
|
|
|
|
|
|