SQL OPENXML and Dynamic Table names

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"
marketboyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sdonthiCommented:
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
marketboyAuthor Commented:
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
Anthony PerkinsCommented:
Please post your Xml.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Anthony PerkinsCommented:
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
Anthony PerkinsCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
marketboyAuthor Commented:
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
Anthony PerkinsCommented:
Make the following tempoary change:

EXEC sp_xml_removedocument @idoc

Print @insertxml
-- EXEC (@insertxml)

And post the result here.
0
Anthony PerkinsCommented:
Also, the CREATE TABLE statement for the table ('.contacts_temp_' + @client1) in the Dynamic SQL statement.
0
marketboyAuthor Commented:
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
Anthony PerkinsCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.