{"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":"\n\n\n
\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\u4e00\u4e0bEmployee\u8868\u91cc\u7684\u6570\u636e<\/p>\n

\"\"<\/p>\n

\u67e5\u8be2\u6bcf\u4e2aUser\u7684\u7684\u76f4\u63a5\u4e0a\u7ea7Manager<\/p>\n

WITH CTE AS( \r\n SELECT UserID,ManagerID,Name,Name AS ManagerName \r\n FROM dbo.Employee \r\n WHERE ManagerID=-1 \r\n UNION ALL \r\n SELECT c.UserID,c.ManagerID,c.Name,p.Name AS ManagerName \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,ManagerName \r\nFROM CTE <\/pre>\n

\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n

\"\"<\/p>\n

\u6211\u4eec\u6765\u89e3\u8bfb\u4e00\u4e0b\u4e0a\u9762\u7684\u4ee3\u7801<\/p>\n

  • \u67e5\u8be2ManagerID=-1\uff0c\u4f5c\u4e3a\u6839\u8282\u70b9\uff0c\u8fd9\u662f\u9012\u5f52\u67e5\u8be2\u7684\u8d77\u59cb\u70b9\u3002<\/li>\n
  • \u8fed\u4ee3\u516c\u5f0f\u662f UNION ALL \u4e0b\u9762\u7684\u67e5\u8be2\u8bed\u53e5\u3002\u5728\u67e5\u8be2\u8bed\u53e5\u4e2d\u8c03\u7528\u4e2dCTE\uff0c\u800c\u67e5\u8be2\u8bed\u53e5\u5c31\u662fCTE\u7684\u7ec4\u6210\u90e8\u5206\uff0c\u5373 \u201c\u81ea\u5df1\u8c03\u7528\u81ea\u5df1\u201d\uff0c\u8fd9\u5c31\u662f\u9012\u5f52\u7684\u771f\u8c1b\u6240\u5728\u3002\u6240\u8c13\u8fed\u4ee3\uff0c\u662f\u6307\u6bcf\u4e00\u6b21\u9012\u5f52\u90fd\u8981\u8c03\u7528\u4e0a\u4e00\u6b21\u67e5\u8be2\u7684\u7ed3\u679c\u96c6\uff0cUNION ALL\u662f\u6307\u6bcf\u6b21\u90fd\u628a\u7ed3\u679c\u96c6\u5e76\u5728\u4e00\u8d77\u3002<\/li>\n
  • \u8fed\u4ee3\u516c\u5f0f\u5229\u7528\u4e0a\u4e00\u6b21\u67e5\u8be2\u8fd4\u56de\u7684\u7ed3\u679c\u96c6\u6267\u884c\u7279\u5b9a\u7684\u67e5\u8be2\uff0c\u76f4\u5230CTE\u8fd4\u56deNULL\u6216\u8fbe\u5230\u6700\u5927\u7684\u8fed\u4ee3\u6b21\u6570\uff0c\u9ed8\u8ba4\u503c\u662f32\u3002\u6700\u7ec8\u7684\u7ed3\u679c\u96c6\u662f\u8fed\u4ee3\u516c\u5f0f\u8fd4\u56de\u7684\u5404\u4e2a\u7ed3\u679c\u96c6\u7684\u5e76\u96c6\uff0c\u6c42\u5e76\u96c6\u662f\u7531UNION ALL \u5b50\u53e5\u5b9a\u4e49\u7684\uff0c\u5e76\u4e14\u53ea\u80fd\u4f7f\u7528UNION ALL<\/li>\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

    \u5176\u4e2dCAST(Name AS NVARCHAR(MAX))\u662f\u5c06Name\u7684\u957f\u5ea6\u8bbe\u7f6e\u4e3a\u6700\u5927\uff0c\u9632\u6b62\u5b57\u6bb5\u8fc7\u957f\u8d85\u51fa\u5b57\u6bb5\u957f\u5ea6\u3002\u5177\u4f53\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n

    \"\"<\/p>\n

    \u4ee5\u4e0a\u5c31\u662f\u9012\u5f52\u67e5\u8be2\u7684\u4e00\u4e9b\u77e5\u8bc6\u4ecb\u7ecd\u4e86\uff0c\u81ea\u5df1\u53ef\u4ee5\u52a8\u624b\u5b9e\u9a8c\u4e00\u4e0b\uff0c\u8fd9\u4e2a\u4e00\u822c\u5728\u9762\u8bd5\u4e2d\u4e5f\u7ecf\u5e38\u4f1a\u8003\u5bdf\u9762\u8bd5\u8005\uff0c\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6~<\/p>\n","protected":false},"excerpt":{"rendered":"

    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 […]<\/p>\n","protected":false},"author":1920,"featured_media":163226,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-224924","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/224924","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1920"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=224924"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/224924\/revisions"}],"predecessor-version":[{"id":225040,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/224924\/revisions\/225040"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/163226"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=224924"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=224924"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=224924"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}