[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

SQL OPENXML and Dynamic Table names

Posted on 2007-04-05
10
Medium Priority
?
488 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
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 1500 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

650 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