• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 56
  • Last Modified:

sql server update query

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
PeterBaileyUk
Asked:
PeterBaileyUk
  • 14
  • 7
  • 2
2 Solutions
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
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
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
0
 
PeterBaileyUkAuthor Commented:
yes it didnt seem right to put it in the same question as the query was returning the rows.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
PeterBaileyUkAuthor Commented:
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
 
PeterBaileyUkAuthor Commented:
i amended slightly to clarify the output
ee.JPG
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
Did you try the update command?
0
 
PeterBaileyUkAuthor Commented:
yes it fails on syntax but cannot see why as mentioned ID: 41748617
0
 
PeterBaileyUkAuthor Commented:
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
 
PeterBaileyUkAuthor Commented:
like this
ee.JPG
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
That's not my code. You added the last 3 lines. They aren't intended to be there.
0
 
PeterBaileyUkAuthor Commented:
aha ok I misunderstood.
0
 
PeterBaileyUkAuthor Commented:
here is the correct snapshot of the screen
ee.JPG
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
Did you run it?
Don't forget to test in a non-production environment.
0
 
PeterBaileyUkAuthor Commented:
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
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
Ok. I didn't have opportunity to test it.
Can you add SELECT keyword before the RTRIM?
0
 
PeterBaileyUkAuthor Commented:
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
 
PeterBaileyUkAuthor Commented:
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
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
Don't add parenthesis:
UPDATE tblwords
 SET StrShort = SELECT RTRIM I.Word + '
...
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
there need to be brackets, actually
either it's

set field = RTRIM((SELECT ...))
or
set field = ( SELECT RTRIM( ...) ... )
0
 
PeterBaileyUkAuthor Commented:
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
 
PeterBaileyUkAuthor Commented:
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
 
PeterBaileyUkAuthor Commented:
Thank you to you both.
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
Oh, missed a pair of parenthesis.
Glad that you sorted it out.
Cheers
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 14
  • 7
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now