Solved

SQL OPENXML and Dynamic Table names

Posted on 2007-04-05
10
446 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Read about achieving the basic levels of HRIS security in the workplace.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

785 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