Solved

SQL OPENXML and Dynamic Table names

Posted on 2007-04-05
10
475 Views
Last Modified: 2010-05-18
I am trying to turn this OpenXML syntax into using a dynamic table name but having some trouble. Any help would be much appreciated.

Declare @iDoc integer

EXEC sp_xml_preparedocument @iDoc OUTPUT, @XmlDoc

declare @insertxml varchar(8000)
set @insertxml = 'INSERT ' + @rdsn + '.contacts_temp_' + @client1 + ' (mail_ref,title, firstname, lastname, email, tracknum, contact_owner, lead_source, sms, fax, skype, msn, yahoo, phone, website, birthdate, sex, email_optin, sms_optin, company, jobtitle, department, address1, address2, address3, town, county, postcode, country, alt_address1, alt_address2, alt_address3, alt_town, alt_county, alt_postcode, alt_country, additional1, additional2, additional3)
SELECT mailref, title, firstname, lastname, email, recnum, contactowner, leadsource, mobile, fax, skype, msn, yahoo, telephone, website, birthdate, sex, emailoptin, smsoptin, company, jobtitle, department, address1, address2, address3, town, county, postcode, country, altaddress1, altaddress2, altaddress3, alttown, altcounty, altpostcode, altcountry, additional1, additional2, additional3
FROM OpenXml(' + @idoc + ', "root/contact", 2)
With (
mailref varchar(150),
title varchar(150),
firstname varchar(150),
lastname varchar(150),
email varchar(500),
recnum varchar(100),
contactowner varchar(100),
leadsource varchar(100),
mobile varchar(100),
fax varchar(100),
skype varchar(150),
msn varchar(150),
yahoo varchar(150),
telephone varchar(150),
website varchar(150),
birthdate varchar(150),
sex varchar(150),
emailoptin bit,
smsoptin bit,
company varchar(150),
jobtitle varchar(150),
department varchar(150),
address1 varchar(150),
address2 varchar(150),
address3 varchar(150),
town varchar(150),
county varchar(150),
postcode varchar(150),
country varchar(150),
altaddress1 varchar(150),
altaddress2 varchar(150),
altaddress3 varchar(150),
alttown varchar(150),
altcounty varchar(150),
altpostcode varchar(150),
altcountry varchar(150),
additional1 varchar(150),
additional2 varchar(150),
additional3 varchar(150))'

EXEC sp_xml_removedocument @idoc

EXEC (@insertxml)

I am getting the error "Syntax error converting the varchar value"
0
Comment
Question by:marketboy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
10 Comments
 
LVL 2

Expert Comment

by:sdonthi
ID: 18858583
Fields emailoptin bit & smsoptin bit are number fields either 1 or 0 and I think you're trying to send character type of data.
0
 

Author Comment

by:marketboy
ID: 18859032
Hi sdonthi,

I'm afraid I don't understand your comment, the emailoptin & smsoptin fields are filled with 1's and 0's but I thought because I had defined it as a bit type it would be ok.

Could you give me an example of what you are talkiing about.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 18859413
Please post your Xml.
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 18859432
But I suspect your problem is that this:
FROM OpenXml(' + @idoc + ', "root/contact", 2)

Should be:
FROM OpenXml(' + CAST(@idoc as varchar(20)) + ', "root/contact", 2)
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
ID: 18859449
Also the XPath should be enclosed in single quotes (two of them as Dynamic SQL) not double quotes as in:

FROM OpenXml(' + CAST(@idoc as varchar(20)) + ', ''root/contact'', 2)
0
 

Author Comment

by:marketboy
ID: 18881430
This has gotten over the converting varchar value but it now is giving me an error: incorrect syntax near '7' which every time it runs returns a different number in an increment of 2. -

Here is an example of the XML file I am parsing:
<?xml version="1.0" encoding="UTF-8"?> <root> <contact> <mailref>DB2DB4BC-E081-5F17-9CF9C2FC26D47995</mailref> <title>Mr</title> <firstname>Chris</firstname> <lastname>Poole</lastname> <email>chris@marketsuite.co.uk</email> <recnum>1</recnum> <contactowner/> <leadsource/> <mobile>4.48E+11</mobile> <fax>123</fax> <skype/> <msn/> <yahoo/> <telephone/> <website/> <birthdate/> <sex/> <emailoptin>1</emailoptin> <smsoptin>1</smsoptin> <company/> <jobtitle/> <department/> <address1>Address1</address1> <address2>Address2</address2> <address3>Address3</address3> <town/> <county/> <postcode/> <country/> <altaddress1/> <altaddress2/> <altaddress3/> <alttown/> <altcounty/> <altpostcode/> <altcountry/> <additional1/> <additional2/> <additional3/> </contact> <contact> <mailref>DB2DB4CB-E081-5F17-9C832B6E98985A2F</mailref> <title/> <firstname/> <lastname/> <email>bill@marketsuite.co.uk</email> <recnum>2</recnum> <contactowner/> <leadsource/> <mobile>4.48E+11</mobile> <fax/> <skype/> <msn/> <yahoo/> <telephone/> <website/> <birthdate/> <sex/> <emailoptin>1</emailoptin> <smsoptin>1</smsoptin> <company/> <jobtitle/> <department/> <address1/> <address2/> <address3/> <town/> <county/> <postcode/> <country/> <altaddress1/> <altaddress2/> <altaddress3/> <alttown/> <altcounty/> <altpostcode/> <altcountry/> <additional1/> <additional2/> <additional3/> </contact> </root>
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 18883850
Make the following tempoary change:

EXEC sp_xml_removedocument @idoc

Print @insertxml
-- EXEC (@insertxml)

And post the result here.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 18883862
Also, the CREATE TABLE statement for the table ('.contacts_temp_' + @client1) in the Dynamic SQL statement.
0
 

Author Comment

by:marketboy
ID: 18888024
Hi AC,

I am sending in variables from coldfusion to SQL stored procedure but nothing is being returned, is there any log in SQL where this information is stored?

The create table statement works fine but did you want me to put the print statement in there as well?
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 18891167
In order for me to understand what is going on I need:
1. The CREATE TABLE statement.
2. The output of the PRINT statement
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question