要來實作一個維護頁面,資料表如下,
這種樣子是很難閱讀,馬上看出各張 FormID 的權限,下面這種整理過的資料在呈現上也比較好閱讀,
本來以為要把資料全部丟到程式中去執行了,沒想到上網 Google 了一下就早到 SQL 就可以辦到了,
SELECT a.[FormID] ,left(a.[Authority],LEN(a.[Authority])-1) AS [Description] FROM (SELECT [FormID],(SELECT CAST([AuthType] AS NVARCHAR ) + ',' FROM [FormAuth] WHERE [FormId] = o.[FormId] FOR XML PATH('')) AS [Authority] FROM [FormAuth] o GROUP BY [FormID]) a ORDER BY [FormID]
省時又省力了
參考: http://weisnote.blogspot.tw/2012/12/t-sql.html