sql to set an xml value - sql

I'm a novice in mySql.
I'm trying to replace a value in the xml column of my table.
my select method works.
SELECT * FROM `comics` WHERE ExtractValue(xml,'comic/pageNumber') = 6
my replace method doesn't. I've been searching for the correct syntax for a bit now...
SET xml.modify(
replace value of ('comic/pageNumber') with 5
)
some background:
this situation comes up when i delete a comic page.
it leaves a gap in the page numbers, after which i would either:
iterate through all the comics and remove any gaps in the page numbers.
or
iterate through all comics with pageNumber larger than the deleted page, and reduce their pageNumber by 1.

How about
UPDATE comics
SET xml = UpdateXML(xml,'comic/pageNumber', '<pageNumber>5</pageNumber>')
WHERE ExtractValue(xml,'comic/pageNumber') = 6

Tested on MySQL version 5.1
UPDATE `comics`
SET xml = UpdateXML(xml,
'comic/pageNumber',
concat('<pageNumber>',(ExtractValue(xml,'comic/pageNumber')+1),'</pageNumber>'))
WHERE ExtractValue(xml,'comic/pageNumber') >= 1

You'd be better off actually storing the fields in the table, rather than a single field with xml in it. Then the following would work. Otherwise there's not much point using a relational database at all.
BEGIN;
DELETE FROM `comics`
WHERE `comicID` = :id AND `pageNumber` = :page;
UPDATE `comics` SET `pageNumber` = `pageNumber` - 1
WHERE `comicID` = :id AND `pageNumber` > :page;
COMMIT;

Related

write a postgreSQL query to update a json column

I have a table T with a column J of type json, and one of it's row contains value
{'SELECTION':'A'}
I want to change it to
{'selection':'A'}
i.e change the key of the JSON.
Any one has experience? I am not able to find any relevant resource.
Thanks to #Sebastian, I solved it using the documentation https://www.postgresql.org/docs/current/functions-json.html
update T set J = '{"selection":"A"}' where J::jsonb #> '{"SELECTION":"A"}'::jsonb
Another query that will do same
update T set J = '{"selection":"A"}' where J::json->>'SELECTION'='A'

Update a Table using a Join

I wish to update a table using, but need to use another table to get the correct field. The new information is not taken from another field from another table.
The following SQL statement returns the correct information:
SELECT PURCHASEHEADER.ORDERNOTES
FROM PURCHASEHEADER, ASSEMBLYLINESOURCE
WHERE ASSEMBLYLINESOURCE.HEADERSYSUNIQUEID = 72637001
AND PURCHASEHEADER.ORDERNUMBER = ASSEMBLYLINESOURCE.PURCHASEORDERNUMBER
I have tried the following:
UPDATE PURCHASEHEADER SET PURCHASEHEADER.ORDERNOTES = 'Updated'
WHERE EXISTS (
SELECT 1 FROM ASSEMBLYLINESOURCE
WHERE PURCHASEHEADER.ORDERNUMBER = ASSEMBLYLINESOURCE.PURCHASEORDERNUMBER
) AND ASSEMBLYLINESOURCE.HEADERSYSUNIQUEID = 72637001
An error is returned saying: " ...Column Unknown ASSEMBLYLINESOURCE.HEADERSYSUNIQUEID..." but it does exist as it works in the first query.
I have seen similar posts from Mark Rotteveel dated July 2017, but still can't get it to work.
There is an issue with your closing bracket. Try this, it worked for me.
UPDATE PURCHASEHEADER set PURCHASEHEADER.ORDERNOTES = 'Updated'
WHERE EXISTS (SELECT 1 FROM ASSEMBLYLINESOURCE WHERE
PURCHASEHEADER.ORDERNUMBER = ASSEMBLYLINESOURCE.PURCHASEORDERNUMBER AND
ASSEMBLYLINESOURCE.HEADERSYSUNIQUEID = 72637001)

SQL updating multiple cells with values from a subquery

The other day,I thought I found a very elegant way to update part of a set of cells. This is what I came up with :
Update mytable
Set ExternalLink = REPLACE (ExternalLink, R.UniqueID, R.ParentUniqueID)
From (
Select u.UniqueID, u.ParentUniqueID, RIGHT (mt.ExternalLink, 7) as EL
From uniqueidtable u
Join mytable mt
On EL = u.ParentUniqueID
Where mt.ExternalLink Like '%stringinurl%' ) R
I believe I got the idea from a stack question, but I can't find it in my history.
I can't seem to get more than one cell updated, even though the subquery returns several rows when tested alone. What's wrong with this and how can I tweak it to update multiple cells at a time?
Thank you for your help.
Okay, I came up with an answer. I'm not sure why a Table Expression did not work in this case, but here's what I came up with:
UPDATE mytable
SET ExternalLink = REPLACE(Ta.ExternalLink, T.ParentUniqueID, T.UniqueID)
FROM mytable Ta
JOIN UniqueIDtable
ON RIGHT(Ta.ExternalLink, 7) = T.ParentUniqueID
WHERE ExternalLink LIKE '%stringinurl%' AND ISNUMERIC(RIGHT(Ta.ExternalLink, 7)) = '1';
It seems I was just messing up the placement of the ID and parent ID in the replace. I also didn't count on the end of the URL string in the ExternalLink column to be anything but a numeric value, so that came up with some issues as well. It seems to be working for me now, so I've got that going for me.

Update field in table for all records using a select statement

A previous developer created a table that stores the absolute path to files in our server. I want to convert them to relative paths instead.
I already wrote the portion that properly strips the string down to a relative path. My issue is understanding how to basically update each record, with a new version of its own string.
Here is what I originally tried:
UPDATE LFRX_Attachments
SET [File] = (SELECT TOP 1 SUBSTRING([File], PATINDEX('%Files\%', [File]) + 6, LEN([File]))
FROM LFRX_Attachments A
WHERE [Type] = 4 AND AttachmentId = A.AttachmentId)
However, this tanked in epic fashion by just overwriting every record to have the value of the first record in the table. Any suggestions?
UPDATE LFRX_Attachments
SET [File] = SUBSTRING([File], PATINDEX('Files\', [File]) + 6, LEN([File]))
WHERE [Type] = 4
From a readability/maintenance standpoint, you're better off selecting for the data you want to alter, then iterating through the result set and updating each record separately.
Does this work for you?
UPDATE LFRX_Attachments SET [File] = SUBSTRING([File], PATINDEX('Files\', [File]) + 6, LEN([File]))

My simple sql update query is not working right :(

I've got a simple sql query that is trying to update a single row. The code came from some Linq-to-sql code (i used Profiler to grab it), but please don't worry about the source (L2S) ... that's irrelivant to the question.
Now, when i try and do an update with the where clause, I get 0 rows updated.
I then try and do a select with the same where clause. i get a single result back. Ok, so the data is there.
It's only when i turn off some of the where clause statements does the query finally work. I just don't get it :(
I can't really explain it. So I made a video showing this.
Please watch the video on YouTube here.
Can anyone help? It's really weird :(
Database is MS-SQL 2008.
-- UPDATE
The sql i'm trying to execute (after i've cleaned it up from L2S)..
update tblBoardPost
set IsSticky = 1
where IdBoardPost = 1278
and IdAddress = 212787
and Subject = N'ttreterte'
and Banner is null
and UniqueSubject = N'ttreterte5'
AND (NOT ([IsAnnouncement] = 1))
AND (NOT ([IsSticky] = 1))
AND (NOT ([IsLocked] = 1))
and IsVisible = 1
and IdUserModified = 1
AND [IdNewsArticle] IS NULL
AND [IdList] IS NULL
(note: yes, i know the NOT (blah = 1)) is weird, but that's L2S that made that.
Update 2:
The code in the video is being ran in a transaction/rollback trans. this means that if i do run one of those weird scenario's that works, the change works .. but the rollback undoes it .. so when i run it a 2nd time .. the original value has been returned to the db row.
Also, in the vid, the select query is not exactly the same as the update query .. because i've been trying so many permutations (with no luck) .. so by the time i made the vid .. i forgot to show the original code. That said, the same code in both the select statement and the update/where statement (when i copy/paste on my machine) reproduces the exact same probs :(
Update 2 :)
As per Lieven's request, here's the screenshot showing the code and then the results.
alt text http://img196.imageshack.us/img196/7111/lieven.png
I checked your video, and you should try the same SQL conditions, that you used to return one row in your select query, in your update query. The conditions in the two SQL statements in your video aren't the same, and I don't really care why; they're not apples to apples.
Just copy and paste the working WHERE clause over the non-working one.
In the video, your select goes like
and not IsAnnouncement = 1
wich gives entirely different results as in your update
and not (IsAnnouncement = 1)
It seems you are suffering from an operator precedence issue .
Edit
as the queries don't match in your video and although you say they have matched could you copy paste following script and let us know if or if they do not give you the same results? (I for one would have a hard time believing that the rows affected would differ)
begin tran
select *
from tblBoardPost
where
IdBoardPost = 1278
and IdAddress = 212787
and Subject = N'ttreterte'
and Banner is null
and UniqueSubject = N'ttreterte5'
AND (NOT ([IsAnnouncement] = 1))
AND (NOT ([IsSticky] = 1))
AND (NOT ([IsLocked] = 1))
and IsVisible = 1
and IdUserModified = 1
AND [IdNewsArticle] IS NULL
AND [IdList] IS NULL
update tblBoardPost
set IsSticky = 1
where
IdBoardPost = 1278
and IdAddress = 212787
and Subject = N'ttreterte'
and Banner is null
and UniqueSubject = N'ttreterte5'
AND (NOT ([IsAnnouncement] = 1))
AND (NOT ([IsSticky] = 1))
AND (NOT ([IsLocked] = 1))
and IsVisible = 1
and IdUserModified = 1
AND [IdNewsArticle] IS NULL
AND [IdList] IS NULL
rollback tran