SQL Server table XML column operation code

Source: Internet
Author: User
The operation code for XML columns in SQLServerTable. For more information, see.

The operation code of XML columns in SQL Server Table. For more information, see.

The Code is as follows:
-- Create a test table
DECLARE @ Users TABLE
(
Id int identity (1, 1 ),
UserInfo XML
)
--- Insert Test Data
DECLARE @ xml XML
SET @ xml ='


1
Test1

'
Insert into @ Users (UserInfo) VALUES (@ xml)
-- Insert a single node (type: as first, as last, after (default), before)
UPDATE @ Users SET UserInfo. modify ('insert shanghai
Into (/root/user) [1] ')
-- Insert multiple nodes separated by commas (,)
UPDATE @ Users SET UserInfo. modify ('insert ( Steven ,
Shi ) Into (/root/user) [1] ')
-- Add attributes
Declare @ editTime varchar (23 );
Set @edittime = CONVERT (VARCHAR (23), GETDATE (), 121 );
UPDATE @ Users SET UserInfo. modify (
N'insert (attribute editTime {SQL: variable ("@ editTime ")})
Into (/root/user/userid) [1]'
)
-- Insert multiple attributes separated by commas (,).
Declare @ aid float, @ bid float
Set @ aid = 0.5
UPDATE @ Users SET UserInfo. modify ('insert (attribute aid {SQL: variable ("@ aid ")},
Attribute bid {"test "}
)
Into (/root/user) [1] ')
--- Insert comments
UPDATE @ Users SET UserInfo. modify (n' insert
Before (/root/user/userid [1]) [1] ')
--- Insert Processing Command
UPDATE @ Users SET UserInfo. modify ('insert
Before (/root) [1] ')
--- Insert CDATA
UPDATE @ Users SET UserInfo. modify (n' insert <city>北京</city> or cdata
After (/root/user) [1] ')
--- Insert text
UPDATE @ Users SET UserInfo. modify (n' insert text {"insert text"} as first
Into (/root/user) [1] ')
--- Insert According to the if Condition Statement
--- Determine attribute values
UPDATE @ Users SET UserInfo. modify ('insert if (/root/user [@ ID = 1]) then ( 888888 )
Else ( 66666 )
Into (/root/user) [1] ')
---- Judge node Value
UPDATE @ Users SET UserInfo. modify ('insert if (/root/user [firstName = "Steven 1"]) then ( 1111 )
Else ( 2222 )
Into (/root/user) [1] ')
---- Determine whether the number of user nodes is less than or equal to 10
UPDATE @ Users SET UserInfo. modify ('insert if (count (/root/user) <= 10) then element user {"This is a new user "}
Else () as last
Into (/root) [1] ')
SELECT * FROM @ Users
-- Actions with namespaces
-- DECLARE @ xml XML
-- SET @ xml ='
--
-- 1
-- Test1
--
-- '
-- Insert into Users (UserInfo) VALUES (@ xml)
-- UPDATE Users SET UserInfo. modify ('
-- Declare namespace UI = "http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/users ";
-- Insert
-- Steven 2
-- As first
-- Into (/UI: root) [1] ')
-- SELECT * FROM Users
-- UPDATE Users SET UserInfo. modify ('
-- Declare namespace UI = "http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/users ";
-- Insert attribute ID {"55 "}
-- Into (/UI: root/UI: user) [1] ')

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.