February 21, 2013 at 10:42 pm
Hi all
i am new to sqlserver 2008 and i trying update table1 column1's value if table1 columns 1 values match with table2 columns2 value
with the new column value from table2
eG
table 1 contains:
column 1:
microsoft outlook 2007,microsoft 2007 outlook,microsoft out look 2007,microsoft 2007 office,microsoft office 2007
plus lots more
table 2 contains:
column 1:
microsoft outlook 2007,microsoft 2007 outlook,microsoft out look 2007,microsoft 2007 office,microsoft office 2007
plus lots more
and in table2:column 2
outlook 2007,office 2007
so the final result should be in table 1, column 1 should say
outlook 2007,office 2007
the query i have is which i do not know if this is the right way in thinking is as follows
-------------------------------------------------------
with C as
(
select distinct RTRIM(LTRIM(TGT.software_name_raw)) as Target_Name,
RTRIM(LTRIM(SRC.software_name_raw)) as Source_Name
from dbo.BigTable as TGT
INNER JOIN dbo.RefTable as SRC
on TGT.software_name_raw = SRC.software_name_raw
)
update C
set Target_Name = Source_Name
--------------------------------------------------------
it is also producing an error saying
Msg 4406, Level 16, State 1, Line 14
Update or insert of view or function 'C' failed because it contains a derived or constant field.
Please help
February 22, 2013 at 12:09 am
It will be better if you provide DDL and sample data in a ready to use format so that volunteers here can copy+paste it in their machine and start working with less effort
The sample data you have provided is not clear enough
Please check the link in my signature if you want to know how to do this
I am sure you have people helping in no time if you follow the article
How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
February 22, 2013 at 12:28 am
I think I get the question but not sure, so here's a shot in the dark:
DECLARE @Table1 TABLE (software_name_raw VARCHAR(100))
INSERT INTO @Table1
SELECT 'microsoft outlook 2007'
UNION ALL SELECT 'microsoft 2007 outlook'
UNION ALL SELECT 'microsoft out look 2007'
UNION ALL SELECT 'microsoft 2007 office'
UNION ALL SELECT 'microsoft office 2007'
;WITH Table2 (software_name_raw, software_name) AS (
SELECT 'microsoft outlook 2007', 'outlook 2007'
UNION ALL SELECT 'microsoft 2007 outlook', 'outlook 2007'
UNION ALL SELECT 'microsoft out look 2007', 'outlook 2007'
UNION ALL SELECT 'microsoft 2007 office', 'office 2007'
UNION ALL SELECT 'microsoft office 2007', 'office 2007')
UPDATE a
SET software_name_raw = software_name
FROM @Table1 a
INNER JOIN Table2 b ON a.software_name_raw = b.software_name_raw
SELECT *
FROM @Table1
The error message you are getting is because you can't update through a CTE when you use DISTINCT or have derived columns (like your LTRIM/RTRIM).
You also may need to take care with this approach if your database happens to be using a case sensitive collation string.
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
February 22, 2013 at 1:13 am
I'm not so sure either - the tables might look like this:
DROP TABLE #Table1
SELECT
*
INTO #Table1
FROM (
SELECT Column1 = 'microsoft outlook 2007,microsoft 2007 outlook,microsoft out look 2007,microsoft 2007 office,microsoft office 2007'
) d
DROP TABLE #Table2
SELECT
*
INTO #Table2
FROM (
SELECT
Column1 = 'microsoft outlook 2007,microsoft 2007 outlook,microsoft out look 2007,microsoft 2007 office,microsoft office 2007',
Column2 = 'outlook 2007,office 2007'
) d
SELECT * FROM #Table1
SELECT * FROM #Table2
in which case it's changed from a trivial exercise to an interesting one 😉
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
February 22, 2013 at 1:33 am
ChrisM@Work (2/22/2013)
I'm not so sure either - the tables might look like this:
DROP TABLE #Table1
SELECT
*
INTO #Table1
FROM (
SELECT Column1 = 'microsoft outlook 2007,microsoft 2007 outlook,microsoft out look 2007,microsoft 2007 office,microsoft office 2007'
) d
DROP TABLE #Table2
SELECT
*
INTO #Table2
FROM (
SELECT
Column1 = 'microsoft outlook 2007,microsoft 2007 outlook,microsoft out look 2007,microsoft 2007 office,microsoft office 2007',
Column2 = 'outlook 2007,office 2007'
) d
SELECT * FROM #Table1
SELECT * FROM #Table2
in which case it's changed from a trivial exercise to an interesting one 😉
As tempting as this is, I won't jump on this challenge until we here back from the OP! 😛
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
February 22, 2013 at 3:18 am
ok uys, 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 be
secondly I hope this can explain it further
if 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 2007
if there is no match then just leave table1's software_name_raw value as it is
I hope this helps you guys as I am now starting to confuse myself further lol
TABLE1-COL1 called software_name_raw
Microsoft Office Visio Viewer 2003 (English)
Microsoft Office Visio 2003 Step by Step
Microsoft Office Visio 2003 Step by Step
Microsoft Office Visio Viewer 2003 (English)
Microsoft Office Visio Viewer 2003 (English)
Microsoft Office Visio 2007 (Exe)
Microsoft® Office Visio® 2007
Microsoft® Office Visio® 2007
Microsoft Office Visio 2007 Professional Edition
Microsoft Office Visio Professional 2007
Microsoft Office Visio Viewer 2007
Microsoft Office Visio Viewer 2007
Microsoft Office Visio Viewer 2007
Microsoft Office Visio Viewer 2007
Microsoft Office Visio 2010
Microsoft Office Visio 2010
Microsoft Office Visio 2010 (Exe)
Microsoft Office Visio 2010 Premium Edition
Microsoft Office Visio 2010 Professional Edition
TABLE2-COL1 also called software_name_rawTABLE2-COL2 called software_name_ammended
Microsoft Office Visio Viewer 2003 (English),Visio 2003 Viewer
Microsoft Office Visio 2003 Step by Step,Visio 2003 Step by Step
Microsoft Office Visio 2003 Step by Step,Visio 2003 Step by Step
Microsoft Office Visio Viewer 2003 (English),Visio 2003 Viewer
Microsoft Office Visio Viewer 2003 (English),Visio 2003 Viewer
Microsoft Office Visio 2007 (Exe),Visio 2007
Microsoft® Office Visio® 2007,Visio 2007
Microsoft® Office Visio® 2007,Visio 2007
Microsoft Office Visio 2007 Professional Edition, Visio 2007 Professional
Microsoft Office Visio Professional 2007,Visio 2007 Professional
Microsoft Office Visio Viewer 2007,Visio 2007 Viewer
Microsoft Office Visio Viewer 2007,Visio 2007 Viewer
Microsoft Office Visio Viewer 2007,Visio 2007 Viewer
Microsoft Office Visio Viewer 2007,Visio 2007 Viewer
Microsoft Office Visio 2010,Visio 2010
Microsoft Office Visio 2010,Visio 2010
Microsoft Office Visio 2010 (Exe),Visio 2010
Microsoft Office Visio 2010 Premium Edition,Visio 2010 Premium
Microsoft Office Visio 2010 Professional Edition,Visio 2010 Professional
February 22, 2013 at 3:33 am
Have you tried the solution I suggested?
I'm thinking it will do what you need.
I hope I didn't confuse you by putting Table2 into the CTE.
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
February 22, 2013 at 3:56 am
dwain.c (2/22/2013)
Have you tried the solution I suggested?I'm thinking it will do what you need.
I hope I didn't confuse you by putting Table2 into the CTE.
Looks good to me. Same as this;
SET NOCOUNT ON
DROP TABLE #TABLE1
CREATE TABLE #TABLE1 (software_name_raw VARCHAR(100))
INSERT INTO #TABLE1 (software_name_raw) SELECT 'Microsoft Office Visio Viewer 2003 (English)'
INSERT INTO #TABLE1 (software_name_raw) SELECT 'Microsoft Office Visio 2003 Step by Step'
INSERT INTO #TABLE1 (software_name_raw) SELECT 'Microsoft Office Visio 2003 Step by Step'
INSERT INTO #TABLE1 (software_name_raw) SELECT 'Microsoft Office Visio Viewer 2003 (English)'
INSERT INTO #TABLE1 (software_name_raw) SELECT 'Microsoft Office Visio Viewer 2003 (English)'
INSERT INTO #TABLE1 (software_name_raw) SELECT 'Microsoft Office Visio 2007 (Exe)'
INSERT INTO #TABLE1 (software_name_raw) SELECT 'Microsoft® Office Visio® 2007'
INSERT INTO #TABLE1 (software_name_raw) SELECT 'Microsoft® Office Visio® 2007'
INSERT INTO #TABLE1 (software_name_raw) SELECT 'Microsoft Office Visio 2007 Professional Edition'
INSERT INTO #TABLE1 (software_name_raw) SELECT 'Microsoft Office Visio Professional 2007'
INSERT INTO #TABLE1 (software_name_raw) SELECT 'Microsoft Office Visio Viewer 2007'
INSERT INTO #TABLE1 (software_name_raw) SELECT 'Microsoft Office Visio Viewer 2007'
INSERT INTO #TABLE1 (software_name_raw) SELECT 'Microsoft Office Visio Viewer 2007'
INSERT INTO #TABLE1 (software_name_raw) SELECT 'Microsoft Office Visio Viewer 2007'
INSERT INTO #TABLE1 (software_name_raw) SELECT 'Microsoft Office Visio 2010'
INSERT INTO #TABLE1 (software_name_raw) SELECT 'Microsoft Office Visio 2010'
INSERT INTO #TABLE1 (software_name_raw) SELECT 'Microsoft Office Visio 2010 (Exe)'
INSERT INTO #TABLE1 (software_name_raw) SELECT 'Microsoft Office Visio 2010 Premium Edition'
INSERT INTO #TABLE1 (software_name_raw) SELECT 'Microsoft Office Visio 2010 Professional Edition'
DROP TABLE #TABLE2
CREATE TABLE #TABLE2 (software_name_raw VARCHAR(100), software_name_amended VARCHAR(100))
INSERT INTO #TABLE2 (software_name_raw,software_name_amended) SELECT 'Microsoft Office Visio Viewer 2003 (English)', 'Visio 2003 Viewer'
INSERT INTO #TABLE2 (software_name_raw,software_name_amended) SELECT 'Microsoft Office Visio 2003 Step by Step', 'Visio 2003 Step by Step'
INSERT INTO #TABLE2 (software_name_raw,software_name_amended) SELECT 'Microsoft Office Visio 2003 Step by Step', 'Visio 2003 Step by Step'
INSERT INTO #TABLE2 (software_name_raw,software_name_amended) SELECT 'Microsoft Office Visio Viewer 2003 (English)', 'Visio 2003 Viewer'
INSERT INTO #TABLE2 (software_name_raw,software_name_amended) SELECT 'Microsoft Office Visio Viewer 2003 (English)', 'Visio 2003 Viewer'
INSERT INTO #TABLE2 (software_name_raw,software_name_amended) SELECT 'Microsoft Office Visio 2007 (Exe)', 'Visio 2007'
INSERT INTO #TABLE2 (software_name_raw,software_name_amended) SELECT 'Microsoft® Office Visio® 2007', 'Visio 2007'
INSERT INTO #TABLE2 (software_name_raw,software_name_amended) SELECT 'Microsoft® Office Visio® 2007', 'Visio 2007'
INSERT INTO #TABLE2 (software_name_raw,software_name_amended) SELECT 'Microsoft Office Visio 2007 Professional Edition', 'Visio 2007 Professional'
INSERT INTO #TABLE2 (software_name_raw,software_name_amended) SELECT 'Microsoft Office Visio Professional 2007', 'Visio 2007 Professional'
INSERT INTO #TABLE2 (software_name_raw,software_name_amended) SELECT 'Microsoft Office Visio Viewer 2007', 'Visio 2007 Viewer'
INSERT INTO #TABLE2 (software_name_raw,software_name_amended) SELECT 'Microsoft Office Visio Viewer 2007', 'Visio 2007 Viewer'
INSERT INTO #TABLE2 (software_name_raw,software_name_amended) SELECT 'Microsoft Office Visio Viewer 2007', 'Visio 2007 Viewer'
INSERT INTO #TABLE2 (software_name_raw,software_name_amended) SELECT 'Microsoft Office Visio Viewer 2007', 'Visio 2007 Viewer'
INSERT INTO #TABLE2 (software_name_raw,software_name_amended) SELECT 'Microsoft Office Visio 2010', 'Visio 2010'
INSERT INTO #TABLE2 (software_name_raw,software_name_amended) SELECT 'Microsoft Office Visio 2010', 'Visio 2010'
INSERT INTO #TABLE2 (software_name_raw,software_name_amended) SELECT 'Microsoft Office Visio 2010 (Exe)', 'Visio 2010'
INSERT INTO #TABLE2 (software_name_raw,software_name_amended) SELECT 'Microsoft Office Visio 2010 Premium Edition', 'Visio 2010 Premium'
INSERT INTO #TABLE2 (software_name_raw,software_name_amended) SELECT 'Microsoft Office Visio 2010 Professional Edition', 'Visio 2010 Professional'
-- Check using SELECT - does everything look ok?
SELECT t1.software_name_raw, t2.software_name_amended
FROM #TABLE1 t1
INNER JOIN #TABLE2 t2
ON t2.software_name_raw = t1.software_name_raw
ORDER BY t1.software_name_raw
-- nope - result set has FAR too many rows
-- Eliminate dupes in Table 2 and check again using SELECT:
SELECT t1.software_name_raw, t2.software_name_amended
FROM #TABLE1 t1
INNER JOIN (
SELECT software_name_raw,software_name_amended
FROM #TABLE2
GROUP BY software_name_raw,software_name_amended
) t2
ON t2.software_name_raw = t1.software_name_raw
ORDER BY t1.software_name_raw
-- looks ok: convert to UPDATE:
UPDATE t1 SET software_name_raw = t2.software_name_amended
FROM #TABLE1 t1
INNER JOIN (
SELECT software_name_raw,software_name_amended
FROM #TABLE2
GROUP BY software_name_raw,software_name_amended
) t2
ON t2.software_name_raw = t1.software_name_raw
-- Check the results:
SELECT * FROM #TABLE1 t1
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
February 22, 2013 at 4:12 am
dwain
fantastic that seems to work fine, will that also cover any null values
much appreciated
alan
my next post is going to be on a bulk insert I am having a problem with for inserting the rows into the database for this query, am i allowed to post in here or would i have to set another post up
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply