如何将xml信息文档插入到现有表中,我无法弄清楚insert语句如何工作我的代码如下:
USE MyGuitarShop;
DECLARE @CustomerUpdate XML;
SET @CustomerUpdate = '
<NewCustomers>
<Customer LastName="Chan" FirstName="Isabella" Password="" EmailAddress="izzychan@yahoo.com"/>
<Customer LastName="Prine" FirstName="John" Password="" EmailAddress="johnprine@gmail.com"/>
<Customer LastName="Kitchen" FirstName="Kathy" Password="" EmailAddress="kathykitchen@sbcglobal.net"/>
</NewCustomers>
'
;
INSERT INTO Customers (LastName, Password, EmailAddress)
VALUES (@CustomerUpdate.value('(/NewCustomers/LastName)[1]', 'varchar(50)'),
(@CustomerUpdate.value('(/NewCustomers/FirstName)[1]', 'varchar(50)'),
(@CustomerUpdate.value('(/NewCustomers/Password)[1]', 'varchar(50)'),
(@CustomerUpdate.value('(/NewCustomers/EmailAddress)[1]', 'varchar(50)');
答案 0 :(得分:3)
您从LastName
选择节点 NewCustomers
,而NewCustomers
仅包含Customer
个节点,其中包含LastName
} attribute。
要选择姓氏,请改用以下查询:
value('(/NewCustomers/Customer/@LastName)[1]', 'varchar(50)')
由于您从单个XML值中提取数据,因此选择非常简单:
declare @CustomerUpdate xml;
set @CustomerUpdate = '
<NewCustomers>
<Customer LastName="Chan" FirstName="Isabella" Password="" EmailAddress="izzychan@yahoo.com"/>
<Customer LastName="Prine" FirstName="John" Password="" EmailAddress="johnprine@gmail.com"/>
<Customer LastName="Kitchen" FirstName="Kathy" Password="" EmailAddress="kathykitchen@sbcglobal.net"/>
</NewCustomers>';
select
t.Customer.value('@LastName', 'nvarchar(50)') as [LastName],
t.Customer.value('@FirstName', 'nvarchar(50)') as [FirstName],
t.Customer.value('@Password', 'nvarchar(50)') as [Password],
t.Customer.value('@EmailAddress', 'nvarchar(50)') as [EmailAddress]
from @CustomerUpdate.nodes('(/NewCustomers/Customer)') t(Customer)
如果您从一行XML值中选择数据,则应使用cross apply
代替。
重要提示:不要在数据库中存储普通文本中的密码。如果你真的这样做,那你做错了。如果您不理解原因,请了解哈希和盐,或者更好的是,让其他人为您处理个人信息:OpenID是将保护敏感数据的责任转移给Google规模公司的方法之一。