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] ')