Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
Clages1
Yak Posting Veteran
69 Posts |
Posted - 2013-01-09 : 07:11:49
|
Hii have a TXT file in fact a XML filei would like thru SQL read this XML file and extract piece of information. sample Xml file below<Person>John Doe</Person><Place>Seattle, WA</Place>i would like to read it and pass 2 parameters<Place> and </Place>and get "Seattle, WA"i am reading http://www.sqlservercentral.com/stairway/92778/but i did not find anything about READING and Extracting nodesany help will be apreciatedtks Clages |
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2013-01-09 : 07:41:51
|
The second parameter (</Place>) is redundant, so you need only one parameter. Also, it is easier if you just pass "Place" rather than "<Place>". Assuming so, here is an example of how you can query:DECLARE @param VARCHAR(32) SET @param = 'Place'DECLARE @x XML;SET @x = '<Person>John Doe</Person> <Place>Seattle, WA</Place>';SELECT c.query('.')FROM @x.nodes('/*[local-name() = sql:variable("@param")]') T(c) This should work in SQL 2005 and above - I am not familiar with the capabilities of Sql XML in SQL 2000, so the this may not work on SQL 2000. |
|
|
Jeff Moden
Aged Yak Warrior
652 Posts |
Posted - 2013-01-09 : 19:50:47
|
quote: Originally posted by Clages1 Hii have a TXT file in fact a XML filei would like thru SQL read this XML file and extract piece of information. sample Xml file below<Person>John Doe</Person><Place>Seattle, WA</Place>i would like to read it and pass 2 parameters<Place> and </Place>and get "Seattle, WA"i am reading http://www.sqlservercentral.com/stairway/92778/but i did not find anything about READING and Extracting nodesany help will be apreciatedtks Clages
Hold the phone a minute. Let's find something out, first. - Are you saying that you have a file on the disk that you'd first like to import into SQL Server?
- Are you really using SQL Server 2000 for this?
--Jeff Moden RBAR is pronounced "ree-bar" and is a "Modenism" for "Row By Agonizing Row".First step towards the paradigm shift of writing Set Based code:"Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column." When writing schedules, keep the following in mind:"If you want it real bad, that's the way you'll likely get it." |
|
|
Clages1
Yak Posting Veteran
69 Posts |
Posted - 2013-01-10 : 12:36:44
|
Hi , I am using SQL2008 the sample of our friend sunitabeckdoesnt work, because it no using a XML file as Inputthe main Ideia is, pass Xml file as Parameter , Node as parameterthen read this XML file and get String between Nodes.SOmething like this SPgetnodes(c:\xxxx\nfe.xml, "<Place>")return "Seattle, WA"this below is a sample XML file<Person>John Doe</Person><Place>Seattle, WA</Place>tksCarlos LagesBrazil |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2013-01-10 : 15:56:43
|
SQL Server and T-SQL may not be the best tool for the job if your objective is to parse files and extract data. If you don't need the results in a T-SQL script, consider using a .Net program. If you do need to get the result inside a T-SQL script, you would need to import the file into a database table using BCP, SSIS or other similar tools and then query against that table. Other alternatives might be to use a CLR stored proc, or xp_cmdshell in combination with a .Net program. |
|
|
Clages1
Yak Posting Veteran
69 Posts |
Posted - 2013-01-11 : 04:54:39
|
Ok, i will drive in this wayTks anywaycarlos LagesBrazil |
|
|
Jeff Moden
Aged Yak Warrior
652 Posts |
Posted - 2013-01-11 : 18:51:52
|
quote: Originally posted by Clages1 Hi , I am using SQL2008 the sample of our friend sunitabeckdoesnt work, because it no using a XML file as Inputthe main Ideia is, pass Xml file as Parameter , Node as parameterthen read this XML file and get String between Nodes.
You first say it's not an XML file and then you say it is. Which is it? It would be better if you posted more complete code so we can tell.--Jeff Moden RBAR is pronounced "ree-bar" and is a "Modenism" for "Row By Agonizing Row".First step towards the paradigm shift of writing Set Based code:"Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column." When writing schedules, keep the following in mind:"If you want it real bad, that's the way you'll likely get it." |
|
|
|
|
|
|
|