{"id":224924,"date":"2021-09-06T08:47:57","date_gmt":"2021-09-06T00:47:57","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=224924"},"modified":"2021-08-23T08:48:52","modified_gmt":"2021-08-23T00:48:52","slug":"sql-transfer","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/sql-transfer.html","title":{"rendered":"SQL\u4e2d\u7684\u9012\u5f52\u7528\u6cd5"},"content":{"rendered":"
\u5bfc\u8bfb<\/td>\n | \u9012\u5f52\u67e5\u8be2\u6ca1\u6709\u663e\u5f0f\u7684\u9012\u5f52\u7ec8\u6b62\u6761\u4ef6\uff0c\u53ea\u6709\u5f53\u7b2c\u4e8c\u4e2a\u9012\u5f52\u67e5\u8be2\u8fd4\u56de\u7a7a\u7ed3\u679c\u96c6\u6216\u662f\u8d85\u51fa\u4e86\u9012\u5f52\u6b21\u6570\u7684\u6700\u5927\u9650\u5236\u65f6\u624d\u505c\u6b62\u9012\u5f52\u3002\u662f\u6307\u9012\u5f52\u6b21\u6570\u4e0a\u9650\u7684\u65b9\u6cd5\u662f\u4f7f\u7528MAXRECURION\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n <\/p>\n \u9012\u5f52\u67e5\u8be2\u539f\u7406<\/strong><\/div>\n SQL Server\u4e2d\u7684\u9012\u5f52\u67e5\u8be2\u662f\u901a\u8fc7CTE(\u8868\u8868\u8fbe\u5f0f)\u6765\u5b9e\u73b0\u3002\u81f3\u5c11\u5305\u542b\u4e24\u4e2a\u67e5\u8be2\uff0c\u7b2c\u4e00\u4e2a\u67e5\u8be2\u4e3a\u5b9a\u70b9\u6210\u5458\uff0c\u5b9a\u70b9\u6210\u5458\u53ea\u662f\u4e00\u4e2a\u8fd4\u56de\u6709\u6548\u8868\u7684\u67e5\u8be2\uff0c\u7528\u4e8e\u9012\u5f52\u7684\u57fa\u7840\u6216\u5b9a\u4f4d\u70b9;\u7b2c\u4e8c\u4e2a\u67e5\u8be2\u88ab\u79f0\u4e3a\u9012\u5f52\u6210\u5458\uff0c\u4f7f\u8be5\u67e5\u8be2\u79f0\u4e3a\u9012\u5f52\u6210\u5458\u7684\u662f\u5bf9CTE\u540d\u79f0\u7684\u9012\u5f52\u5f15\u7528\u662f\u89e6\u53d1\u3002\u5728\u903b\u8f91\u4e0a\u53ef\u4ee5\u5c06CTE\u540d\u79f0\u7684\u5185\u90e8\u5e94\u7528\u7406\u89e3\u4e3a\u524d\u4e00\u4e2a\u67e5\u8be2\u7684\u7ed3\u679c\u96c6\u3002<\/p>\n \u9012\u5f52\u67e5\u8be2\u7684\u7ec8\u6b62\u6761\u4ef6<\/strong><\/div>\n \u9012\u5f52\u67e5\u8be2\u6ca1\u6709\u663e\u5f0f\u7684\u9012\u5f52\u7ec8\u6b62\u6761\u4ef6\uff0c\u53ea\u6709\u5f53\u7b2c\u4e8c\u4e2a\u9012\u5f52\u67e5\u8be2\u8fd4\u56de\u7a7a\u7ed3\u679c\u96c6\u6216\u662f\u8d85\u51fa\u4e86\u9012\u5f52\u6b21\u6570\u7684\u6700\u5927\u9650\u5236\u65f6\u624d\u505c\u6b62\u9012\u5f52\u3002\u662f\u6307\u9012\u5f52\u6b21\u6570\u4e0a\u9650\u7684\u65b9\u6cd5\u662f\u4f7f\u7528MAXRECURION\u3002<\/p>\n \u9012\u5f52\u67e5\u8be2\u7684\u4f18\u70b9<\/strong><\/div>\n \u6548\u7387\u9ad8\uff0c\u5927\u91cf\u6570\u636e\u96c6\u4e0b\uff0c\u901f\u5ea6\u6bd4\u7a0b\u5e8f\u7684\u67e5\u8be2\u5feb\u3002<\/p>\n \u9012\u5f52\u7684\u5e38\u89c1\u5f62\u5f0f<\/strong><\/div>\n WITH CTE AS (<\/p>\n SELECT column1,column2... FROM tablename WHERE conditions<\/p>\n UNION ALL<\/p>\n SELECT column1,column2... FROM tablename<\/p>\n INNER JOIN CTE ON conditions<\/p>\n )<\/p>\n \u9012\u5f52\u67e5\u8be2\u793a\u4f8b<\/strong><\/div>\n \u521b\u5efa\u6d4b\u8bd5\u6570\u636e\uff0c\u6709\u4e00\u4e2a\u5458\u5de5\u8868Employee\uff0cManagerID\u662fUserID\u7684\u7236\u8282\u70b9\uff0c\u8fd9\u662f\u4e00\u4e2a\u975e\u5e38\u7b80\u5355\u7684\u5c42\u6b21\u7ed3\u6784\u6a21\u578b\u3002<\/p>\n USE SQL_Road \r\nGO \r\nCREATE TABLE Employee \r\n( \r\n UserID INT, \r\n ManagerID INT, \r\n Name NVARCHAR(10) \r\n) \r\n INSERT INTO dbo.Employee \r\n SELECT 1,-1,N'Boss' \r\n UNION ALL \r\n SELECT 11,1,N'A1' \r\n UNION ALL \r\n SELECT 12,1,N'A2' \r\n UNION ALL \r\n SELECT 13,1,N'A3' \r\n UNION ALL \r\n SELECT 111,11,N'B1' \r\n UNION ALL \r\n SELECT 112,11,N'B2' \r\n UNION ALL \r\n SELECT 121,12,N'C1' <\/pre>\n \u67e5\u8be2\u8def\u5f84<\/strong><\/div>\n \u4e0b\u9762\u6211\u4eec\u901a\u8fc7\u5c42\u6b21\u7ed3\u6784\u67e5\u8be2\u5b50\u8282\u70b9\u5230\u7236\u8282\u70b9\u7684PATH\uff0c\u6211\u4eec\u5bf9\u4e0a\u9762\u7684\u4ee3\u7801\u7a0d\u4f5c\u4fee\u6539\uff1a<\/p>\n WITH CTE AS( \r\n SELECT UserID,ManagerID,Name,CAST(Name AS NVARCHAR(MAX)) AS LPath \r\n FROM dbo.Employee \r\n WHERE ManagerID=-1 \r\n UNION ALL \r\n SELECT c.UserID,c.ManagerID,c.Name,p.LPath+'->'+c.Name AS LPath \r\n FROM CTE P \r\n INNER JOIN dbo.Employee c ON p.UserID=c.ManagerID \r\n ) \r\n \r\nSELECT UserID,ManagerID,Name,LPath \r\nFROM CTE <\/pre>\n |