group_concat
複製MySQL的> SQL Server 2005缺少MySQL中發現的方便group_concat
功能,在需要群組中的連線值時會挑戰。雖然自訂功能提供了解決方案,但對於不熟悉其實作的人來說,它們可能很麻煩。
>一個實用的替代方法是利用SQL Server的系統表和FOR XML PATH
<code class="language-sql">SELECT table_name, LEFT(column_names , LEN(column_names )-1) AS column_names FROM information_schema.columns AS extern CROSS APPLY ( SELECT column_name + ',' FROM information_schema.columns AS intern WHERE extern.table_name = intern.table_name FOR XML PATH('') ) pre_trimmed (column_names) GROUP BY table_name, column_names;</code>
這些方法有效地模仿
<code class="language-sql">WITH extern AS (SELECT DISTINCT table_name FROM INFORMATION_SCHEMA.COLUMNS) SELECT table_name, LEFT(y.column_names, LEN(y.column_names) - 1) AS column_names FROM extern CROSS APPLY (SELECT column_name + ',' FROM INFORMATION_SCHEMA.COLUMNS AS intern WHERE extern.table_name = intern.table_name FOR XML PATH(''), TYPE) x (column_names) CROSS APPLY (SELECT x.column_names.value('.', 'NVARCHAR(MAX)')) y(column_names);</code>
以上是如何在SQL Server 2005中模擬MySQL的group_concat?的詳細內容。更多資訊請關注PHP中文網其他相關文章!