SQL Server 2008 ã® HierarchyID ãã¼ã¿å
SQL Server 2008 ããã¯ãHierarchyID ãã¼ã¿åããµãã¼ãããã¾ãããHierarchyID ãã¼ã¿åã¯ãé層ï¼Hierarchyï¼ã®ãã¹ãåå¾ï¼æä½ãå¯è½ãªãã¼ã¿åã§ãã
次ã®ãããªè¦ªåé層ããã£ã社å¡ãã¼ãã«ï¼ãä¸å¸ç¤¾å¡çªå·ãåã«ä¸å¸ã®ç¤¾å¡çªå·ãæ ¼ç´ããã¦ããï¼ã§èª¬æãã¾ãã
SQL Server 2005 ã§ã¯ããã®ãããªé層ãã¼ã¿ã«å¯¾ãã¦ãCTEï¼Common Table Expression: å ±éãã¼ãã«å¼ï¼ãå©ç¨ãã¦ãå帰ã¯ã¨ãªãå®è¡ãããã¨ã§ãé層ã¬ãã«ãåå¾ãããã¨ãã§ãã¾ãããããã¯æ¬¡ã®ããã«è¨è¿°ãã¾ãã
-- SQL Server 2005 ã®å帰ã¯ã¨ãªã®å ´å WITH cte (社å¡çªå·, 社å¡å, ä¸å¸ç¤¾å¡çªå·, é層) AS ( -- ä¸å¸ SELECT 社å¡çªå·, 社å¡å, ä¸å¸ç¤¾å¡çªå·, 0 FROM ç¤¾å¡ WHERE 社å¡çªå· = 1001 UNION ALL -- é¨ä¸ï¼åå¸°ï¼ SELECT e.社å¡çªå·, e.社å¡å, e.ä¸å¸ç¤¾å¡çªå·, M.é層 + 1 FROM ç¤¾å¡ AS e INNER JOIN cte AS M ON e.ä¸å¸ç¤¾å¡çªå· = M.社å¡çªå· ) SELECT * FROM cte
SQL Server 2008 ããã¯ããã®ã¯ã¨ãªã«å¯¾ã㦠HierarchyID ãã¼ã¿åãå©ç¨ãããã¨ã§ãé層ã®ãã¹ãåå¾ã§ããããã«ãªãã¾ããããã¯æ¬¡ã®ããã«è¨è¿°ãã¾ãã
WITH cte (path, 社å¡çªå·, 社å¡å, ä¸å¸ç¤¾å¡çªå·, é層) AS ( -- ä¸å¸ SELECT HierarchyID::GetRoot() AS root ,社å¡çªå·, 社å¡å, ä¸å¸ç¤¾å¡çªå·, 0 FROM ç¤¾å¡ WHERE 社å¡çªå·= 1001 UNION ALL -- é¨ä¸ï¼åå¸°ï¼ SELECT CAST(M.path.ToString() + CAST(e.社å¡çªå·AS varchar(30)) + '/' AS HierarchyID ) , e.社å¡çªå·, e.社å¡å, e.ä¸å¸ç¤¾å¡çªå·, M.é層+ 1 FROM 社å¡AS e INNER JOIN cte AS M ON e.ä¸å¸ç¤¾å¡çªå·= M.社å¡çªå· ) SELECT path.GetLevel(), path.ToString(), * FROM cte
HierarchyID ãã¼ã¿åã®å©ç¨æ¹æ³ã¯ããªã³ã©ã¤ã³ ããã¯ã®ãã¡ãã«è¨è¼ããã¦ãã¾ãã
http://msdn.microsoft.com/ja-jp/library/bb677173.aspx
ãã®ã»ãã® SQL Server 2008 ã®æ°æ©è½ã®å
·ä½çãªå©ç¨æ¹æ³ã«ã¤ãã¦ã¯ãå¼ç¤¾å·çã® SQL Server 2008 èªç¿æ¸ã·ãªã¼ãºï¼ä¸è¨ URLï¼ã®ãSQL Server 2008 ã®æ³¨ç®ã®æ°æ©è½ãã¤ãæ©ã試ãã¦ã¿ãã! ãç·¨ãåèã«ãã¦ã¿ã¦ãã ããã
http://www.microsoft.com/japan/sqlserver/2008/self-learning/default.mspx