Solved

sql server update query

Posted on 2016-08-09
23
43 Views
Last Modified: 2016-08-09
With Guy and stefans help I have a query that returns ConcatWordText I would like to amend it so that StrShort field in the tblwords(w)
is updated with that result.

use Dictionary
go

WITH    Filtered
          AS ( SELECT   W.Word, w.WordPosition, w.clientcode, w.ClientCodeWordPosition, w.StrShort
                    
               FROM     tblwords w
                        LEFT JOIN tblwordtags wt ON W.clientcodeWordPosition = wt.clientcodeWordPosition
               WHERE    wt.word IS NULL and w.ClientCodeWordPosition like '95052350%'
			   
             )
    SELECT  o.ClientCodeWordPosition, o.clientcode, o.StrShort,
            RTRIM(( SELECT  I.Word + ' '
                    FROM    Filtered I
                    WHERE   I.clientcode = O.clientcode
				 order by i.WordPosition
                  FOR
                    XML PATH('')
                  )) AS ConcatWordText, o.WordPosition
    FROM    Filtered O
   order by o.WordPosition

Open in new window

0
Comment
Question by:PeterBaileyUk
  • 14
  • 7
  • 2
23 Comments
 
LVL 49

Accepted Solution

by:
Vitor Montalvão earned 250 total points
ID: 41748594
I'm not into your database model but I guess you want something like:
use Dictionary
go

WITH Filtered
AS ( SELECT W.Word, w.WordPosition, w.clientcode, w.ClientCodeWordPosition, w.StrShort
    FROM tblwords w
		LEFT JOIN tblwordtags wt ON W.clientcodeWordPosition = wt.clientcodeWordPosition
    WHERE wt.word IS NULL and w.ClientCodeWordPosition like '95052350%')

UPDATE tblwords
SET StrShort = RTRIM(SELECT I.Word + ' '
			FROM Filtered I
			WHERE I.clientcode = tblwords.clientcode
			ORDER BY i.WordPosition
			FOR XML PATH(''))

Open in new window

NOTE: Before running this code in a Production environment please run it in a test database to validate the results are the expected ones.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 41748595
0
 

Author Comment

by:PeterBaileyUk
ID: 41748600
yes it didnt seem right to put it in the same question as the query was returning the rows.
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

Author Comment

by:PeterBaileyUk
ID: 41748617
its complaining in the last select by the rtrim all names are red, i suspect its bracketing in there

use Dictionary
go

WITH    Filtered
          AS ( SELECT   W.Word, w.WordPosition, w.clientcode, w.ClientCodeWordPosition, w.StrShort
                    
               FROM     tblwords w
                        LEFT JOIN tblwordtags wt ON W.clientcodeWordPosition = wt.clientcodeWordPosition
               WHERE    wt.word IS NULL and w.ClientCodeWordPosition like '95052350%'
			   
             )

			 update TblWords


   SET StrShort = RTRIM (SELECT  I.Word + ' '
                    FROM    Filtered I
                    WHERE   I.clientcode = O.clientcode
				 order by i.WordPosition
                  FOR
                    XML PATH('')
                  )) AS ConcatWordText, o.WordPosition
    FROM    Filtered O
   order by o.WordPosition);

Open in new window

0
 

Author Comment

by:PeterBaileyUk
ID: 41748628
i amended slightly to clarify the output
ee.JPG
0
 
LVL 49

Expert Comment

by:Vitor Montalvão
ID: 41748631
Did you try the update command?
0
 

Author Comment

by:PeterBaileyUk
ID: 41748633
yes it fails on syntax but cannot see why as mentioned ID: 41748617
0
 

Author Comment

by:PeterBaileyUk
ID: 41748636
the select from and where and some brackets are underlined:

SET StrShort = RTRIM (SELECT  I.Word + ' '
                    FROM    Filtered I
                    WHERE   I.clientcode = O.clientcode
                         order by i.WordPosition
                  FOR
                    XML PATH('')
                  )) AS ConcatWordText, o.WordPosition
    FROM    Filtered O
   order by o.WordPosition);
0
 

Author Comment

by:PeterBaileyUk
ID: 41748637
like this
ee.JPG
0
 
LVL 49

Expert Comment

by:Vitor Montalvão
ID: 41748645
That's not my code. You added the last 3 lines. They aren't intended to be there.
0
 

Author Comment

by:PeterBaileyUk
ID: 41748655
aha ok I misunderstood.
0
 

Author Comment

by:PeterBaileyUk
ID: 41748660
here is the correct snapshot of the screen
ee.JPG
0
 
LVL 49

Expert Comment

by:Vitor Montalvão
ID: 41748664
Did you run it?
Don't forget to test in a non-production environment.
0
 

Author Comment

by:PeterBaileyUk
ID: 41748673
it wont run it says:
Msg 156, Level 15, State 1, Line 11
Incorrect syntax near the keyword 'SELECT'.
Msg 102, Level 15, State 1, Line 15
Incorrect syntax near ')'.
0
 
LVL 49

Expert Comment

by:Vitor Montalvão
ID: 41748687
Ok. I didn't have opportunity to test it.
Can you add SELECT keyword before the RTRIM?
0
 

Author Comment

by:PeterBaileyUk
ID: 41748692
Ive changed to this but its now :
UPDATE tblwords
SET StrShort = SELECT (RTRIM I.Word + ' '

Msg 156, Level 15, State 1, Line 11
Incorrect syntax near the keyword 'SELECT'.
Msg 102, Level 15, State 1, Line 11
Incorrect syntax near 'I'.
0
 

Author Comment

by:PeterBaileyUk
ID: 41748694
and tried this:

UPDATE tblwords
SET StrShort = SELECT (RTRIM (I.Word + ' ')
                  FROM Filtered I
                  WHERE I.clientcode = tblwords.clientcode
                  ORDER BY i.WordPosition
                  FOR XML PATH(''))
0
 
LVL 49

Expert Comment

by:Vitor Montalvão
ID: 41748698
Don't add parenthesis:
UPDATE tblwords
 SET StrShort = SELECT RTRIM I.Word + '
...
0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 250 total points
ID: 41748718
there need to be brackets, actually
either it's

set field = RTRIM((SELECT ...))
or
set field = ( SELECT RTRIM( ...) ... )
0
 

Author Comment

by:PeterBaileyUk
ID: 41748735
its looking better, nothing underlined now, it says;

Msg 1033, Level 15, State 1, Line 14
The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP, OFFSET or FOR XML is also specified.

I suspect its the last error now
use Dictionary
go

WITH Filtered
AS ( SELECT W.Word, w.WordPosition, w.clientcode, w.ClientCodeWordPosition, w.StrShort
    FROM tblwords w
		LEFT JOIN tblwordtags wt ON W.clientcodeWordPosition = wt.clientcodeWordPosition
    WHERE wt.word IS NULL and w.ClientCodeWordPosition like '95052350%')

UPDATE tblwords
SET StrShort = RTRIM((SELECT I.Word + ' '
			FROM Filtered I
			WHERE I.clientcode = tblwords.clientcode
			ORDER BY i.WordPosition))
			FOR XML PATH('')

Open in new window

0
 

Author Comment

by:PeterBaileyUk
ID: 41748746
I did this and i believe, but i will check again that its worked ok.

UPDATE tblwords
SET StrShort = RTRIM((SELECT I.Word + ' '
			FROM Filtered I
			WHERE I.clientcode = tblwords.clientcode
			ORDER BY i.WordPosition

			FOR XML PATH('')))

Open in new window

0
 

Author Closing Comment

by:PeterBaileyUk
ID: 41748762
Thank you to you both.
0
 
LVL 49

Expert Comment

by:Vitor Montalvão
ID: 41748837
Oh, missed a pair of parenthesis.
Glad that you sorted it out.
Cheers
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

685 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