{"id":969,"date":"2025-01-15T00:40:57","date_gmt":"2025-01-14T16:40:57","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=969"},"modified":"2025-01-15T00:40:57","modified_gmt":"2025-01-14T16:40:57","slug":"%e5%a6%82%e4%bd%95%e8%a7%a3%e5%86%b3-sql-server-%e4%b8%ad%e7%9a%84-%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%88%96%e4%ba%8c%e8%bf%9b%e5%88%b6%e6%95%b0%e6%8d%ae%e5%b0%86%e8%a2%ab%e6%88%aa%e6%96%ad","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/01\/15\/%e5%a6%82%e4%bd%95%e8%a7%a3%e5%86%b3-sql-server-%e4%b8%ad%e7%9a%84-%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%88%96%e4%ba%8c%e8%bf%9b%e5%88%b6%e6%95%b0%e6%8d%ae%e5%b0%86%e8%a2%ab%e6%88%aa%e6%96%ad\/","title":{"rendered":"\u5982\u4f55\u89e3\u51b3 SQL Server \u4e2d\u7684 \u2018\u5b57\u7b26\u4e32\u6216\u4e8c\u8fdb\u5236\u6570\u636e\u5c06\u88ab\u622a\u65ad\u2019 \u9519\u8bef\uff1f"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u5728 SQL Server \u4e2d\uff0c\u9519\u8bef <code>'\u5b57\u7b26\u4e32\u6216\u4e8c\u8fdb\u5236\u6570\u636e\u5c06\u88ab\u622a\u65ad'<\/code>\uff08Error: 8152\uff09\u901a\u5e38\u53d1\u751f\u5728\u63d2\u5165\u6216\u66f4\u65b0\u6570\u636e\u65f6\uff0c\u5f53\u6570\u636e\u8d85\u51fa\u4e86\u76ee\u6807\u5217\u7684\u6570\u636e\u7c7b\u578b\u6216\u5927\u5c0f\u9650\u5236\u3002\u8fd9\u4e2a\u9519\u8bef\u63d0\u793a\u610f\u5473\u7740\u4f60\u5c1d\u8bd5\u5c06\u4e00\u4e2a\u957f\u5ea6\u8d85\u8fc7\u5217\u5b9a\u4e49\u7684\u503c\u63d2\u5165\u5230\u8be5\u5217\u4e2d\uff0c\u5bfc\u81f4\u6570\u636e\u88ab\u622a\u65ad\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u9700\u8981\u5206\u6790\u548c\u8c03\u6574\u8868\u7684\u7ed3\u6784\u3001\u6570\u636e\u7c7b\u578b\u6216\u63d2\u5165\u7684\u503c\uff0c\u907f\u514d\u8d85\u51fa\u9650\u5236\u3002<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u7406\u89e3\u9519\u8bef\u7684\u539f\u56e0<\/strong><\/h3>\n\n\n\n<p>\u5047\u8bbe\u6709\u4e00\u4e2a\u8868 <code>Employees<\/code>\uff0c\u5176\u4e2d <code>FirstName<\/code> \u5217\u5b9a\u4e49\u4e3a <code>VARCHAR(10)<\/code>\uff0c\u5982\u679c\u4f60\u5c1d\u8bd5\u63d2\u5165\u4e00\u4e2a\u8d85\u8fc7 10 \u4e2a\u5b57\u7b26\u7684\u540d\u5b57\uff0c\u5c31\u4f1a\u89e6\u53d1\u8fd9\u4e2a\u9519\u8bef\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\uff0c\u4ee5\u4e0b SQL \u8bed\u53e5\u4f1a\u5bfc\u81f4 <code>'\u5b57\u7b26\u4e32\u6216\u4e8c\u8fdb\u5236\u6570\u636e\u5c06\u88ab\u622a\u65ad'<\/code> \u9519\u8bef\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE Employees (\n    EmployeeID INT,\n    FirstName VARCHAR(10)\n);\n\nINSERT INTO Employees (EmployeeID, FirstName)\nVALUES (1, 'Alexander');<\/code><\/pre>\n\n\n\n<p>\u56e0\u4e3a <code>'Alexander'<\/code> \u7684\u957f\u5ea6\u4e3a 9 \u4e2a\u5b57\u7b26\uff0c\u8d85\u51fa\u4e86 <code>VARCHAR(10)<\/code> \u5217\u7684\u6700\u5927\u957f\u5ea6\u3002\u5c3d\u7ba1\u5b83\u6b63\u597d\u9002\u5e94 10 \u4e2a\u5b57\u7b26\u7684\u9650\u5236\uff0c\u4f46\u5982\u679c\u63d2\u5165\u7684\u5b57\u7b26\u8d85\u8fc7\u5217\u5b9a\u4e49\u7684\u5927\u5c0f\uff0c\u5c31\u4f1a\u629b\u51fa\u6b64\u9519\u8bef\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>\u5e38\u89c1\u5bfc\u81f4\u6b64\u9519\u8bef\u7684\u60c5\u5f62<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u63d2\u5165\u6570\u636e\u8d85\u51fa\u5217\u5927\u5c0f<\/strong>\uff1a\u5982\u679c\u63d2\u5165\u7684\u5b57\u7b26\u4e32\u957f\u5ea6\u8d85\u8fc7\u76ee\u6807\u5217\u7684\u6700\u5927\u5b57\u7b26\u957f\u5ea6\u3002<\/li>\n\n\n\n<li><strong>\u66f4\u65b0\u6570\u636e\u8d85\u51fa\u5217\u5927\u5c0f<\/strong>\uff1a\u5982\u679c\u66f4\u65b0\u64cd\u4f5c\u5bfc\u81f4\u67d0\u5217\u7684\u503c\u8d85\u51fa\u8be5\u5217\u5b9a\u4e49\u7684\u5927\u5c0f\u3002<\/li>\n\n\n\n<li><strong>\u4e0d\u5339\u914d\u6570\u636e\u7c7b\u578b<\/strong>\uff1a\u5c06\u67d0\u4e9b\u6570\u636e\u7c7b\u578b\u7684\u503c\uff08\u5982\u5b57\u7b26\u4e32\u6216\u4e8c\u8fdb\u5236\u6570\u636e\uff09\u63d2\u5165\u5230\u5927\u5c0f\u4e0d\u8db3\u7684\u5217\u4e2d\uff0c\u6216\u8005\u6570\u636e\u7c7b\u578b\u672c\u8eab\u5c31\u4e0d\u517c\u5bb9\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>\u89e3\u51b3\u65b9\u6848<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">a. <strong>\u68c0\u67e5\u5e76\u4fee\u590d\u5217\u7684\u6570\u636e\u7c7b\u578b\u548c\u5927\u5c0f<\/strong><\/h4>\n\n\n\n<p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u68c0\u67e5\u8868\u4e2d\u76ee\u6807\u5217\u7684\u6570\u636e\u7c7b\u578b\u548c\u5927\u5c0f\uff0c\u786e\u4fdd\u5b83\u4eec\u9002\u5e94\u5c06\u8981\u63d2\u5165\u6216\u66f4\u65b0\u7684\u6570\u636e\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528 <code>sp_help<\/code> \u6216\u67e5\u8be2 <code>INFORMATION_SCHEMA.COLUMNS<\/code> \u6765\u67e5\u770b\u8868\u7684\u5217\u5b9a\u4e49\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  EXEC sp_help 'Employees';<\/code><\/pre>\n\n\n\n<p>\u6216\u8005\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH\n  FROM INFORMATION_SCHEMA.COLUMNS\n  WHERE TABLE_NAME = 'Employees';<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u5217\u7684\u957f\u5ea6\u4e0d\u8db3\uff0c\u53ef\u4ee5\u8003\u8651\u4fee\u6539\u5217\u7684\u5b9a\u4e49\uff0c\u589e\u52a0\u5176\u5927\u5c0f\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u901a\u8fc7 <code>ALTER TABLE<\/code> \u4fee\u6539\u5217\u7684\u5927\u5c0f\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  ALTER TABLE Employees\n  ALTER COLUMN FirstName VARCHAR(50);<\/code><\/pre>\n\n\n\n<p>\u8fd9\u5c06\u628a <code>FirstName<\/code> \u5217\u7684\u957f\u5ea6\u4ece <code>VARCHAR(10)<\/code> \u589e\u52a0\u5230 <code>VARCHAR(50)<\/code>\uff0c\u5141\u8bb8\u63d2\u5165\u66f4\u957f\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">b. <strong>\u9a8c\u8bc1\u63d2\u5165\u6570\u636e\u7684\u957f\u5ea6<\/strong><\/h4>\n\n\n\n<p>\u5728\u6267\u884c\u63d2\u5165\u6216\u66f4\u65b0\u64cd\u4f5c\u65f6\uff0c\u786e\u4fdd\u6570\u636e\u7684\u957f\u5ea6\u7b26\u5408\u5217\u7684\u6700\u5927\u9650\u5236\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>LEN()<\/code> \u51fd\u6570\u6765\u68c0\u67e5\u6570\u636e\u7684\u5b9e\u9645\u957f\u5ea6\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT LEN('Alexander');  -- \u8fd4\u56de 9<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u63d2\u5165\u7684\u5b57\u7b26\u4e32\u957f\u5ea6\u5927\u4e8e\u76ee\u6807\u5217\u7684\u5b9a\u4e49\u957f\u5ea6\uff0c\u63d0\u524d\u622a\u65ad\u6216\u4fee\u6539\u6570\u636e\u957f\u5ea6\uff0c\u4ee5\u907f\u514d\u9519\u8bef\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">c. <strong>\u4f7f\u7528 <code>SUBSTRING<\/code> \u6216 <code>LEFT<\/code> \u51fd\u6570\u622a\u65ad\u6570\u636e<\/strong><\/h4>\n\n\n\n<p>\u5982\u679c\u4e0d\u60f3\u4fee\u6539\u8868\u7684\u7ed3\u6784\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>SUBSTRING<\/code> \u6216 <code>LEFT<\/code> \u51fd\u6570\u5728\u63d2\u5165\u4e4b\u524d\u622a\u65ad\u6570\u636e\uff0c\u4ee5\u9002\u5e94\u5217\u7684\u957f\u5ea6\u9650\u5236\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>INSERT INTO Employees (EmployeeID, FirstName)\nVALUES (1, LEFT('Alexander', 10));  -- \u5c06 'Alexander' \u622a\u65ad\u4e3a 'Alexande'<\/code><\/pre>\n\n\n\n<p>\u8fd9\u79cd\u65b9\u6cd5\u5c06\u786e\u4fdd\u63d2\u5165\u7684\u6570\u636e\u4e0d\u8d85\u51fa\u5217\u7684\u5b9a\u4e49\u957f\u5ea6\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">d. <strong>\u68c0\u67e5\u5176\u4ed6\u7ea6\u675f<\/strong><\/h4>\n\n\n\n<p>\u6709\u65f6\uff0c\u9664\u4e86\u6570\u636e\u7c7b\u578b\u548c\u5927\u5c0f\u7684\u9650\u5236\u5916\uff0c\u8868\u4e2d\u8fd8\u53ef\u80fd\u6709\u5176\u4ed6\u7ea6\u675f\uff08\u5982 <code>CHECK<\/code> \u7ea6\u675f\u6216\u5916\u952e\u7ea6\u675f\uff09\uff0c\u5b83\u4eec\u53ef\u80fd\u5f71\u54cd\u6570\u636e\u7684\u63d2\u5165\u3002\u5982\u679c\u6709\u5176\u4ed6\u7ea6\u675f\uff0c\u4e5f\u9700\u8981\u786e\u4fdd\u6570\u636e\u7b26\u5408\u8fd9\u4e9b\u7ea6\u675f\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">e. <strong>\u4f7f\u7528 <code>TRY...CATCH<\/code> \u5757\u6355\u83b7\u9519\u8bef<\/strong><\/h4>\n\n\n\n<p>\u5982\u679c\u4f60\u5e0c\u671b\u5728\u51fa\u73b0\u8fd9\u79cd\u9519\u8bef\u65f6\u8fdb\u884c\u5904\u7406\u5e76\u9632\u6b62\u7a0b\u5e8f\u4e2d\u65ad\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>TRY...CATCH<\/code> \u8bed\u53e5\u5757\u6765\u6355\u83b7\u8be5\u9519\u8bef\uff0c\u5e76\u8fdb\u884c\u9002\u5f53\u7684\u9519\u8bef\u5904\u7406\u6216\u65e5\u5fd7\u8bb0\u5f55\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>BEGIN TRY\n    INSERT INTO Employees (EmployeeID, FirstName)\n    VALUES (1, 'Alexander');\nEND TRY\nBEGIN CATCH\n    PRINT '\u9519\u8bef\u53d1\u751f\uff1a' + ERROR_MESSAGE();\nEND CATCH;<\/code><\/pre>\n\n\n\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5e2e\u52a9\u4f60\u6355\u83b7\u5e76\u5904\u7406\u9519\u8bef\uff0c\u907f\u514d\u56e0\u5355\u4e00\u6570\u636e\u9519\u8bef\u5bfc\u81f4\u6574\u4e2a\u64cd\u4f5c\u5931\u8d25\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<p>\u89e3\u51b3 SQL Server \u4e2d\u7684 <code>'\u5b57\u7b26\u4e32\u6216\u4e8c\u8fdb\u5236\u6570\u636e\u5c06\u88ab\u622a\u65ad'<\/code> \u9519\u8bef\u7684\u5173\u952e\u5728\u4e8e\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u68c0\u67e5\u76ee\u6807\u5217\u7684\u6570\u636e\u7c7b\u578b\u548c\u5927\u5c0f\uff0c\u786e\u4fdd\u5b83\u4eec\u80fd\u5bb9\u7eb3\u63d2\u5165\u7684\u503c\u3002<\/li>\n\n\n\n<li>\u786e\u4fdd\u63d2\u5165\u6216\u66f4\u65b0\u7684\u6570\u636e\u957f\u5ea6\u4e0d\u8d85\u8fc7\u5217\u7684\u5b9a\u4e49\u957f\u5ea6\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u9700\u8981\uff0c\u4f7f\u7528 <code>ALTER TABLE<\/code> \u8c03\u6574\u5217\u7684\u5927\u5c0f\uff0c\u6216\u8005\u5728\u63d2\u5165\u6570\u636e\u65f6\u622a\u65ad\u5b57\u7b26\u4e32\u4ee5\u9002\u5e94\u5217\u7684\u5927\u5c0f\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528\u9002\u5f53\u7684\u9519\u8bef\u5904\u7406\u673a\u5236\u6765\u6355\u83b7\u5e76\u5904\u7406\u6f5c\u5728\u7684\u9519\u8bef\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u6709\u6548\u907f\u514d\u548c\u89e3\u51b3 <code>'\u5b57\u7b26\u4e32\u6216\u4e8c\u8fdb\u5236\u6570\u636e\u5c06\u88ab\u622a\u65ad'<\/code> \u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 SQL Server \u4e2d\uff0c\u9519\u8bef &#8216;\u5b57\u7b26\u4e32\u6216\u4e8c\u8fdb\u5236\u6570\u636e\u5c06\u88ab\u622a\u65ad&#8217;\uff08Error: 8152\uff09\u901a\u5e38\u53d1\u751f\u5728\u63d2\u5165\u6216\u66f4 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-969","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/969","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=969"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/969\/revisions"}],"predecessor-version":[{"id":970,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/969\/revisions\/970"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=969"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=969"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=969"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}