Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Dynamic SQL for variable TableName with OpenXML


Dynamic SQL for variable TableName with OpenXML

Author
Message
Brooke Wilke
Brooke Wilke
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 32
Here is my current code. Because I have the table name as a variable, it is my understanding that I need to use Dynamic SQL. But whenever I use dynamic SQL it causes a problem with the OpenXML statement. Can anyone shed some light on how this code should be written?

SELECT @SQL = 'DECLARE @strXML varchar(8000)
SELECT @strXML = TranRq
FROM ' + @BrSumDesc + '
WHERE EJTranID = ''' + @ejtranno + ''' and lower(transactioncode) in (''tlrckmoneyord'')
DECLARE @xmldocpointer int

EXEC sp_xml_preparedocument @xmldocpointer OUTPUT, @strXML
SELECT [' + @BRSumDesc + '].EJTranID AS EJTranNo,
Rtrim(ProcDateM) + ''/'' + Rtrim(ProcDateD) + ''/'' + ProcDateY as ProcDate
FROM OpenXml(@xmldocpointer, ''/IFX/MonSvcRq/MoneyOrdSellAddRq/TellerData'', 2)
WITH (ProcDateM Char(2) ''./ProcDate/Month'',
ProcDateD Char(2) ''./ProcDate/Day'',
ProcDateY Char(4) ''./ProcDate/Year''
)
join [' + @BrSumDesc + '] on EJTranID = ''' + @ejtranno + '''
EXEC sp_xml_removedocument @xmldocpointer'
PRINT @SQL
EXEC (@SQL)
Tim ffitch
Tim ffitch
Valued Member
Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)

Group: General Forum Members
Points: 53 Visits: 308
Has the XML you are trying to work with got a namespace defined? If it has you have to set up the namespace in the sp_xml_preparedocument arguments.

My first and only foray into XML taught me this after much hair extraction!

Tim
Matt Miller (#4)
Matt Miller (#4)
SSCertifiable
SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)

Group: General Forum Members
Points: 7641 Visits: 18084
Brooke -

could you attach a sample of the input file and what you're trying to get out of it. It would help to decipher what it is you're trying to get out of it.

also - what's generating the XML input? Do you have any control over it?

----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search