Solved

SQL OPENXML and Dynamic Table names

Posted on 2007-04-05
10
424 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
  • 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

747 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now