J'ai vu sur a website, un gars avait retourné ses informations de table de base de données comme une table html. C'était plutôt propre. Son code est le suivant:Table SQL Server arbitraire au tableau XHTML
select 2 [@cellpadding]
,2 [@cellspacing]
,1 [@border]
--This returns the header
,(select th
from (select 'Database Table' th
union all
select 'Entity Count'
union all
select 'Total Rows') d --name of this alias is irrelevant but you have to have one
for xml path(''),type) tr --tr here defines that the header row will be a tr element
--This returns the rows in the table
,(select (select dbtable [*] for xml path('td'),type),
(select entities [*] for xml path('td'),type),
(select rows [*] for xml path('td'),type)
from (--We have to use a derived table because we are grouping
select dbtable = object_name(object_id),
entities = count(distinct name),
rows = count(*)
from sys.columns
group by object_name(object_id)) data
--name of this alias is irrelevant but you have to have one
for xml path ('tr'),type) --path('tr') turns each row into a tr element
for xml path('table'), type
Je voudrais savoir s'il est possible de retourner une table HTML à partir d'une table arbitraire SQL Server. Je sais que les gens ont des opinions divergentes sur la question de savoir s'il existe ou non un meilleur moyen de le faire, mais ce n'est pas le but de cette question. Je suis nouveau à SQL et j'essaye juste d'apprendre mon chemin autour de lui. Mon code est le suivant.
select dataid as [@id], *
from table_1
for xml path('tr'), root('table')
Il retourne quelque chose comme:
<table>
<tr id="1">
<column_1>data</column_1>
<column_2>data</column_2>
</tr>
<tr id="2">
<column_1>data</column_1>
<column_2>data</column_2>
</tr>
</table>
Je préférerais sortie comme:
<table>
<tr id="1">
<td>data</td>
<td>data</td>
</tr>
<tr id="2">
<td>data</td>
<td>data</td>
</tr>
</table>
Est-ce faisable dans une transaction SQL Server? Si oui, comment cela serait-il fait?
C'est en effet moche. Votre requête plus simple a retourné quelque chose de facile à convertir dans le format requis par XSLT. Ce serait une bien meilleure approche. –
Ne vous inquiétez pas, je ne vais pas l'utiliser. C'était un exercice de curiosité. – kzh