简体   繁体   English

更新SQL表列中包含的xml属性的值

[英]Update value of xml attribute contained in a SQL table column

sqlcmd -S .\SQLEXPESS
USE Directory
    SELECT Info FROM dbo.User WHERE Name = TestACC
    SET [User Status].modify('replace value of 
    (/User/[User Status]/text())[1] with ("Activated")')
    GO

I have a database, Directory . 我有一个数据库Directory

Table, dbo.User dbo.User

In the table there are multiple columns but the two im concerned with are Name and Info . 该表中有多列,但与IM有关的两个是NameInfo

Name contains the account username and Info has ~100 lines of xml tags for that users account preferences and settings. Name包含帐户用户名, Info包含约100行xml标记,用于表示该用户的帐户首选项和设置。 One of the lines is <User Status> Deactivated </User Status> or it may be set to <User Status> Activated </User Status> depending on if someone forgot to deactivate the account afterwards. 其中一行是<User Status> Deactivated </User Status> ,也可以将其设置为<User Status> Activated </User Status>具体取决于之后是否忘记了停用该帐户。

I have to change this account status probably 30 times a day by loading a GUI, logging in and changing the status through its properties. 我必须每天通过加载GUI,登录并通过其属性更改状态,大概每天更改30次此帐户状态。 ~6 minutes per status change since the GUI loads soooo slow and there are 3 passwords on rotation to log into the GUI so its a guessing game. 由于GUI加载速度太慢,每次状态更改大约需要6分钟,并且旋转时需要输入3个密码才能登录GUI,因此这是一个猜谜游戏。 The GUI isnt changing any time soon so I'd rather just drop a script to either set user status to deactivated or activated. GUI不会很快改变,因此我只想删除一个脚本以将用户状态设置为停用或激活。

Am I doing this right? 我这样做对吗?

In my opinion, what could be useful here is to create a stored procedure with XML parameters. 我认为,在这里有用的是使用XML参数创建存储过程。

You should mark columns in your table with input parameters and depending on the id of Users where the change occurred the value in User Status will be updated automatically. 您应该使用输入参数标记表中的列,并根据发生更改的“用户”的ID来自动更新“用户状态”中的值。

For more details please take a look at this link TSQL: How to use XML parameters in stored procedures 有关更多详细信息,请查看此链接TSQL:如何在存储过程中使用XML参数

Your problem is: You are dealing with something looking like XML, but this is not valid XML. 您的问题是:您正在处理看起来像XML的东西,但这不是有效的XML。 Check the naming rules here . 在此处检查命名规则

An element like this is not allowed: 不允许这样的元素:

 <User Status> Deactivated </User Status>

You might change this to 您可以将其更改为

<User Status="Deactivated"/>

Or to 或者

<User_Status> Deactivated </User_Status>

Your code is entirely wrong actually: 您的代码实际上是完全错误的:

USE Directory
    SELECT Info FROM dbo.User WHERE Name = TestACC
    SET [User Status].modify('replace value of 
    (/User/[User Status]/text())[1] with ("Activated")')

Besides the invalid XML you would have to use something along 除了无效的XML,您还必须使用一些其他内容

UPDATE dbo.User SET Info.modify('replace value of (someXpathHere)[1] with "SomeValue"');

UPDATE UPDATE

It is really annoying when developers do such stupid things. 开发人员做这种愚蠢的事情真的很烦人。 Now you have to deal with that mess. 现在,您必须处理这种混乱。 Sometimes we have to live with it... 有时我们不得不忍受...

As your "XML" isn't valid XML acutally, you have two choices: 由于您的“ XML”实际上不是有效的XML,因此您有两种选择:

  1. Change it to XML (with string methods like REPLACE ) and deal with XML methods (and remove your changes afterwards) or 将其更改为XML(使用类似REPLACE字符串方法)并处理XML方法(然后删除您的更改)或
  2. Deal with it as a string. 将其作为字符串处理。

I'd suggest to try a simple 我建议尝试一个简单的

UPDATE dbo.User 
SET Info=REPLACE(Info
                ,'<User Status> Deactivated </User Status>'
                ,'<User Status> Activated </User Status>');

This approach depends on the exact writing, other usage of spaces etc would break this... You might think about some RegEx... 这种方法取决于确切的书写方式,其他使用空格等方式可能会破坏此效果...您可能会想到一些RegEx ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM