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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 import html page into SQL

Author  Topic 

mavershang
Posting Yak Master

111 Posts

Posted - 2009-04-08 : 15:57:59
Hi all. Here is my question:
I have a html page which contain a large table. The code is like:
</tr>
<tr><TD COLSPAN=4></td></tr>
<tr><TD COLSPAN=4></td></tr>
<tr align=center><td rowspan=5>9</td><td><a href=Model-16SH9.html#9>9:25</a></td><td> </td><td bgcolor=red> </td></tr>

<tr align=center><td><a href=Model-16SH9.html#10>10:24</a></td><td> </td><td bgcolor=red> </td></tr>
<tr align=center><td><a href=Model-16SH9.html#11>11:23</a></td><td> </td><td bgcolor=red> </td></tr>
<tr align=center><td><a href=Model-16SH9.html#12>12:22</a></td><td> </td><td bgcolor=red> </td></tr>
<tr align=center><td><a href=Model-16SH9.html#13>13:21</a></td><td> </td><td bgcolor=black> </td></tr>
........

Now I want to import this table into SQL but, unfortunately, I do not know much about html. Is there any suggestion to achieve it?

Thanks.

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-04-08 : 16:07:08
You first have to decide what data to insert into the table.
The complete html code, or just the data.



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

mavershang
Posting Yak Master

111 Posts

Posted - 2009-04-08 : 16:08:12
I just want to insert the table, not the whole html page.

quote:
Originally posted by Peso

You first have to decide what data to insert into the table.
The complete html code, or just the data.



E 12°55'05.63"
N 56°04'39.26"


Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-04-08 : 16:20:00
Ok, the table DATA or the table html definition?



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

mavershang
Posting Yak Master

111 Posts

Posted - 2009-04-08 : 16:21:57
Thanks for your response. I do not know much about HTML, but I guess I need the table DATA.

Could you please explain the difference between them?

quote:
Originally posted by Peso

Ok, the table DATA or the table html definition?



E 12°55'05.63"
N 56°04'39.26"


Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-04-08 : 16:26:43
[code]DECLARE @d XML

SET @d = '
<table>
<tr><TD COLSPAN="4"></TD></tr>
<tr><TD COLSPAN="4"></TD></tr>
<tr align="center"><td rowspan="5">9</td><td><a href="Model-16SH9.html#9">9:25</a></td><td> </td><td bgcolor="red"> </td></tr>
<tr align="center"><td><a href="Model-16SH9.html#10">10:24</a></td><td> </td><td bgcolor="red"> </td></tr>
<tr align="center"><td><a href="Model-16SH9.html#11">11:23</a></td><td> </td><td bgcolor="red"> </td></tr>
<tr align="center"><td><a href="Model-16SH9.html#12">12:22</a></td><td> </td><td bgcolor="red"> </td></tr>
<tr align="center"><td><a href="Model-16SH9.html#13">13:21</a></td><td> </td><td bgcolor="black"> </td></tr>
</table>
'

SELECT g.value('@href', 'VARCHAR(MAX)') AS href,
g.value('.', 'VARCHAR(MAX)') AS a
FROM @d.nodes('/table/tr/td/a') AS f(g)
WHERE g.value('.', 'VARCHAR(MAX)') IS NOT NULL[/code]


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

mavershang
Posting Yak Master

111 Posts

Posted - 2009-04-08 : 16:41:17
Thanks a lot.

quote:
Originally posted by Peso

DECLARE	@d XML

SET @d = '
<table>
<tr><TD COLSPAN="4"></TD></tr>
<tr><TD COLSPAN="4"></TD></tr>
<tr align="center"><td rowspan="5">9</td><td><a href="Model-16SH9.html#9">9:25</a></td><td> </td><td bgcolor="red"> </td></tr>
<tr align="center"><td><a href="Model-16SH9.html#10">10:24</a></td><td> </td><td bgcolor="red"> </td></tr>
<tr align="center"><td><a href="Model-16SH9.html#11">11:23</a></td><td> </td><td bgcolor="red"> </td></tr>
<tr align="center"><td><a href="Model-16SH9.html#12">12:22</a></td><td> </td><td bgcolor="red"> </td></tr>
<tr align="center"><td><a href="Model-16SH9.html#13">13:21</a></td><td> </td><td bgcolor="black"> </td></tr>
</table>
'

SELECT g.value('@href', 'VARCHAR(MAX)') AS href,
g.value('.', 'VARCHAR(MAX)') AS a
FROM @d.nodes('/table/tr/td/a') AS f(g)
WHERE g.value('.', 'VARCHAR(MAX)') IS NOT NULL



E 12°55'05.63"
N 56°04'39.26"


Go to Top of Page

mavershang
Posting Yak Master

111 Posts

Posted - 2009-04-08 : 16:52:02
A stupid question: I notice that you change the html code such as
COLSPAN=4 to COLSPAN="4". But if my html is pretty long, how could I do that?

Thanks


quote:
Originally posted by Peso

DECLARE	@d XML

SET @d = '
<table>
<tr><TD COLSPAN="4"></TD></tr>
<tr><TD COLSPAN="4"></TD></tr>
<tr align="center"><td rowspan="5">9</td><td><a href="Model-16SH9.html#9">9:25</a></td><td> </td><td bgcolor="red"> </td></tr>
<tr align="center"><td><a href="Model-16SH9.html#10">10:24</a></td><td> </td><td bgcolor="red"> </td></tr>
<tr align="center"><td><a href="Model-16SH9.html#11">11:23</a></td><td> </td><td bgcolor="red"> </td></tr>
<tr align="center"><td><a href="Model-16SH9.html#12">12:22</a></td><td> </td><td bgcolor="red"> </td></tr>
<tr align="center"><td><a href="Model-16SH9.html#13">13:21</a></td><td> </td><td bgcolor="black"> </td></tr>
</table>
'

SELECT g.value('@href', 'VARCHAR(MAX)') AS href,
g.value('.', 'VARCHAR(MAX)') AS a
FROM @d.nodes('/table/tr/td/a') AS f(g)
WHERE g.value('.', 'VARCHAR(MAX)') IS NOT NULL



E 12°55'05.63"
N 56°04'39.26"


Go to Top of Page

revdnrdy
Posting Yak Master

220 Posts

Posted - 2009-04-08 : 17:19:52
Hello;

I don't recommend storing your table structure inside SQL from a design or performance standpoint. I am curious why someone would do that. If its simply because you are new to html then I would abandon this approach of storing everything on the sql server.

The original poster said he wanted the 'table data' to be stored (not necessarily the structure). Storing the structure is unnecessary and incurs more processing overhead (ie.. parsing). It could also become a maintenance headache.

The better approach would be to code the page structure independent of the data. Then you use a scripting language to retrieve the data from the database and populate your table. A similar approach can be used to update the database from a webpage.

You said you 'didn't know much about html'. I think it is worth the time for you to learn how to get data to and from a webpage from SQL.


r&r
Go to Top of Page

mavershang
Posting Yak Master

111 Posts

Posted - 2009-04-08 : 17:33:59
Thanks for your post.

I have never working with html before. So when I first met this problem, what I thought was to write a perl code, which I am familiar with, to retrieve the data I want and then make a SQL "insert" to create the table in SQL. But obviously that is not so intuitive. So I asked whether there is any other way to import that table.


quote:
Originally posted by revdnrdy

Hello;

I don't recommend storing your table structure inside SQL from a design or performance standpoint. I am curious why someone would do that. If its simply because you are new to html then I would abandon this approach of storing everything on the sql server.

The original poster said he wanted the 'table data' to be stored (not necessarily the structure). Storing the structure is unnecessary and incurs more processing overhead (ie.. parsing). It could also become a maintenance headache.

The better approach would be to code the page structure independent of the data. Then you use a scripting language to retrieve the data from the database and populate your table. A similar approach can be used to update the database from a webpage.

You said you 'didn't know much about html'. I think it is worth the time for you to learn how to get data to and from a webpage from SQL.


r&r


Go to Top of Page

revdnrdy
Posting Yak Master

220 Posts

Posted - 2009-04-08 : 19:34:39
quote:
Originally posted by mavershang

Thanks for your post.

I have never working with html before. So when I first met this problem, what I thought was to write a perl code, which I am familiar with, to retrieve the data I want and then make a SQL "insert" to create the table in SQL. But obviously that is not so intuitive. So I asked whether there is any other way to import that table.



Don't use SQL insert to build an html table (its not good design).

Do it this way..

1. Build your webpage as a combination of html and scripting (Perl, C#, Visual Basic or whatever). That page resides on your web server.

2. Use SQL to retrieve data from your database and insert it into the fields of the table. You will embed the sql commands inside of the scripting language.

There are numerous examples on the web of how to retrieve data from an sql server and publish it into a webpage.

It is relatively easy to understand and well worth the effort to learn. You can easily code dynamically driven web pages with this approach..

r&r





Go to Top of Page
   

- Advertisement -