Click here to Skip to main content
15,898,752 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I want to insert values from grid to database. I found that the best way to insert is through xml.
I am using stored procedure to insert values from stored procedure.

My xml tag is

" <ROOT> 
<Price propertyid="362" startdate="16/06/2011" enddate="25/06/2011" weeklyrate="120" orderseq="1"</Price>
 <Price propertyid="362" startdate="26/06/2011" enddate="08/07/2011" weeklyrate="140" orderseq="2"</Price>
</ROOT> "



My stored stored procedure is

SQL
ALTER     PROCEDURE [dbo].[InsertShortTermPrice]
 
@xmlDoc varchar(MAX)
AS
 declare @hDoc int
 BEGIN TRANSACTION
    
    EXEC sp_xml_PrepareDocument    @hDoc OUT, @XMLDoc   
	INSERT INTO shortTerm_pricelist 
	SELECT propertyid,startdate,enddate,weeklyrate,orderseq 
	FROM OPENXML(@hDoc,'/Record/Price',1)
	WITH(propertyid int '@propertyid',startdate datetime '@startdate',enddate datetime '@enddate',weeklyrate int '@weeklyrate',orderseq int '@orderseq')
   
    COMMIT TRANSACTION
    
    EXEC sp_xml_removeDocument     @hDoc 



But i am getting error as
"The error description is 'Required white space was missing.'. Could not find prepared statement with handle 0. sp_xml_removedocument: The value supplied for parameter number 1 is invalid. The statement has been terminated."
Can anyone help please
Posted
Updated 10-Jun-11 3:18am
v3
Comments
Dave Kreskowiak 10-Jun-11 9:18am    
Removed VB.NET tag since this has nothing to do with VB.NET.

Your XML is not well formed I believe. From what you posted I can see that "Price" nodes are missing ">" before the close tag.
 
Share this answer
 
Hi...

Try this link.


http://www.codeproject.com/KB/database/DataTableAsXMLToDataBase.aspx[^]

And also check this

SQL
ALTER PROCEDURE [dbo].[InsertShortTermPrice]

@xmlDoc varchar(MAX)
AS
declare @hDoc int
BEGIN TRANSACTION

EXEC sp_xml_PrepareDocument @hDoc OUT, @XMLDoc
INSERT INTO shortTerm_pricelist
SELECT propertyid,startdate,enddate,weeklyrate,orderseq
SELECT
	x.item.value('@propertyid[1]', 'Bigint') AS propertyid,
	x.item.value('@startdate[1]', 'Datetime') AS startdate,
	x.item.value('@enddate[1]', 'Datetime') AS enddate,
	x.item.value('@weeklyrate[1]', 'Bigint') AS weeklyrate,
	x.item.value('@orderseq[1]', 'Bigint') AS orderseq
FROM @hDoc.nodes('//Record/Price') AS x(item)
 

COMMIT TRANSACTION
 
Share this answer
 
v3
Comments
Mythri_8 10-Jun-11 10:45am    
I modified my stored procedure as below
GO
ALTER PROCEDURE [dbo].[InsertShortTermPrice]

@xmlDoc xml

AS
BEGIN


INSERT INTO [shortTerm_pricelist]
SELECT
CAST(COLX.query.value('data(property_id)') as bigint) as propertyid,
CAST(COLX.query.value('data(start_date)') as datetime) as [start_date],
CAST(COLX.query.value('data(end_date)') as datetime) as end_date,
CAST(COLX.query.value('data(weekly_rate)') as datetime) as weekly_rate,
CAST(COLX.query.value('data(orderSeq)') as datetime) as orderSeq
FROM @xmlDoc.nodes('DocumentElement/ShortTermPrice') as TABX(COLX)


END


But i am getting error as: "Cannot find either column "COLX" or the user-defined function or aggregate "COLX.query.value", or the name is ambiguous."
Can i know y am i getting this error

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900