Conversion failed when converting the varchar value to data type int in SQL Server -


i have problem. have 2 identical code inserting data xml table in sql server.

my first code:

declare    @ndoc int,    @cxml xml  select @cxml = '<root><rekening nomorcif=''123'' nomorrek=''123456789'' statusrek=''1'' matauang=''idr'' saldo=''1000''></rekening></root>'  exec sp_xml_preparedocument @ndoc output, @cxml  insert [50732rekening_tm] select * openxml (@ndoc, 'root/rekening', 1)  (nomorcif varchar(30), nomorrek varchar(30), statusrek varchar(30), matauang varchar(30), saldo varchar(30))  exec sp_xml_removedocument @ndoc 

this code runs smoothly

but code returns error

declare     @ndoc int,     @cxml xml,     @query nvarchar(max)  select @cxml ='<root><rekening nomorcif=''123'' nomorrek=''123456789'' statusrek=''1'' matauang=''idr'' saldo=''1000''></rekening></root>'  exec sp_xml_preparedocument @ndoc output, @cxml  set @query = 'insert [50732rekening_tm] select * openxml ('+@ndoc+', ''root/rekening'', 1) (nomorcif varchar(30), nomorrek varchar(30), statusrek varchar(30), matauang varchar(30), saldo varchar(30))'  exec sp_executesql @query exec sp_xml_removedocument @ndoc 

the error

msg 245, level 16, state 1, line 15
conversion failed when converting varchar value 'insert [50732rekening_tm] select * openxml (' data type int.

why second code contain error? need don't know how fix it.

thanks

you must pass handle of xml document parameter

declare     @ndoc int,     @cxml xml,     @query nvarchar(max)  select @cxml ='<root><rekening nomorcif=''123'' nomorrek=''123456789'' statusrek=''1'' matauang=''idr'' saldo=''1000''></rekening></root>'  exec sp_xml_preparedocument @ndoc output, @cxml  set @query = n'insert [50732rekening_tm] select * openxml (@ndoc, ''root/rekening'', 1) (nomorcif varchar(30), nomorrek varchar(30), statusrek varchar(30), matauang varchar(30), saldo varchar(30))' exec sp_executesql @query, n'@ndoc int', @ndoc = @ndoc exec sp_xml_removedocument @ndoc 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -