我正在尝试插入数据VIA XML格式。同样的XML格式定义如下&我想在SQL表中逐行插入字段。但是,没有地址插入SQL表中,只有客户信息插入虚拟表中。
<XML>
<Customer>
<NAME>YOGESH</NAME>
<CONTACT>YOGESH SHARMA</CONTACT>
<Mobile>123456789</Mobile>
<Status>A</Status>
<MALE>1</MALE>
<Add>
<ADD1>
<Address>AHMEDABAD</Address>
<State>GUJARAT</State>
<City>AHMEDABAD</City>
<Pincode>380016</Pincode>
</ADD1>
<ADD2>
<Address>RAJKOT</Address>
<State>GUJARAT</State>
<City>RAJKOT</City>
<Pincode>360001</Pincode>
</ADD2>
</Add>
</Customer>
</XML>
我的SP如下:
ALTER PROCEDURE [dbo].[OPENXMLDUMMY]
@xmlCustomer NTEXT
AS
BEGIN
DECLARE @DOC INT;
EXEC sp_xml_preparedocument
@DOC OUTPUT,
@xmlCustomer;
INSERT INTO Dummy
(Name,
Contact,
Mobile,
Status,
Male,
InsertDate
)
SELECT XML.NAME,
XML.Contact,
XML.Mobile,
XML.Status,
XML.Male,
GETDATE()
FROM OPENXML(@DOC, '/XML/Customer', 2) WITH(Name VARCHAR(50),
Contact VARCHAR(75), Mobile BIGINT, Status VARCHAR(10), Male VARCHAR(10),
InsertDate DATETIME) XML;
INSERT INTO DummyExtd
(
ID,
Address,
State,
City,
Pincode
) SELECT (SELECT ID FROM DUMMY WHERE Name = Name),
XML.Address,
XML.State,
XML.City,
XML.Pincode
FROM OPENXML(@DOC, '/XML/Customer/Add',2) WITH (ID INT, Address
VARCHAR(50), State VARCHAR(50), City VARCHAR(50), Pincode INT) XML;
EXEC sp_xml_removedocument @DOC;
END;
所以,我只想在SQL表中插入以下格式的数据:
ID Name Contact Mobile Status Male InsertDate
1 YOGESH YOGESH SHARMA 123456789 A 1 2017-07-26 13:28:30.957
ID Address State City Pincode
1 AHMEDABAD GUJARAT AHMEDABAD 380016
1 RAJKOT GUJARAT RAJKOT 360001
那么,我当前存储过程中的问题是什么?需要纠正它。
感谢你 约杰什
答案 0 :(得分:5)
我在这里做了一个相同的演示。请仔细研究。
首先,我创建了两个表,即Customer(您的表名Dummy)和Customer_Address(您的表名DummyText)。它们看起来像下面的按扣。
表客户
表: Customer_Address
以下是您更新的商店程序。
ALTER PROCEDURE [dbo].[OPENXMLDUMMY]
@xmlCustomer NTEXT
AS
BEGIN
DECLARE @DOC INT;
Declare @CustId INT;
EXEC sp_xml_preparedocument
@DOC OUTPUT,
@xmlCustomer;
INSERT INTO Customer(Name, Contact, Mobile, Status, Male, InsertDate)
SELECT XML.[NAME], XML.Contact, XML.Mobile, XML.Status, XML.Male, GETDATE() AS InsertDate
FROM OPENXML(@DOC, '/XML/Customer', 2) WITH(NAME VARCHAR(50), CONTACT VARCHAR(75), Mobile BIGINT, Status VARCHAR(10), MALE VARCHAR(10), InsertDate DATETIME) XML;
SET @CustId = SCOPE_IDENTITY()
INSERT INTO Customer_Address(Cust_Id, Address, State, City, Pincode)
SELECT @CustId AS Cust_Id, XML.Address, XML.State, XML.City, XML.Pincode
FROM OPENXML(@DOC, '/XML/Customer/Add/ADD1',2) WITH
(
Address VARCHAR(50),
State VARCHAR(50),
City VARCHAR(50),
Pincode INT
) XML;
INSERT INTO Customer_Address(Cust_Id, Address, State, City, Pincode)
SELECT @CustId AS Cust_Id, XML.Address, XML.State, XML.City, XML.Pincode
FROM OPENXML(@DOC, '/XML/Customer/Add/ADD2',2) WITH
(
Address VARCHAR(50),
State VARCHAR(50),
City VARCHAR(50),
Pincode INT
) XML;
EXEC sp_xml_removedocument @DOC;
END
使用此过程,我执行了您的示例xml数据,它看起来就像下面两个表中的条目一样。
答案 1 :(得分:0)
您的问题在
(SELECT ID FROM DUMMY WHERE Name = Name)
它返回DUMMY
表中的所有值,并导致错误。
子查询返回的值超过1。这是不允许的 子查询跟随=,!=,&lt;,&lt; =,&gt;,&gt; =或当子查询用作 表达。
解决方案:
-- After the first insert
Declare @DummyId int = scope_identity() -- get new inserted id from dummy
INSERT INTO DummyExtd
(
ID,
Address,
State,
City,
Pincode
) SELECT
@DummyId,
XML.Address,
XML.State,
XML.City,
XML.Pincode
FROM OPENXML(@DOC, '/XML/Customer/Add',2) WITH
( -- ID INT, --- Remove it, ID tag doesn't exist in your xml
Address VARCHAR(50),
State VARCHAR(50),
City VARCHAR(50),
Pincode INT
) XML;