{"id":138907,"date":"2024-10-31T16:20:19","date_gmt":"2024-10-31T08:20:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/138907\/"},"modified":"2024-10-31T16:20:19","modified_gmt":"2024-10-31T08:20:19","slug":"sql-server%e5%bb%ba%e7%ab%8b%e7%b2%be%e7%b7%bb%e7%9a%84%e8%b2%a8%e5%b9%a3%e5%9e%8b%e6%8f%9b%e7%ae%97%e7%b3%bb%e7%b5%b1%ef%bc%88sqlserver%e8%b2%a8%e5%b9%a3%e5%9e%8b%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/138907\/","title":{"rendered":"SQL Server\u5efa\u7acb\u7cbe\u7dfb\u7684\u8ca8\u5e63\u578b\u63db\u7b97\u7cfb\u7d71\uff08sqlserver\u8ca8\u5e63\u578b\uff09"},"content":{"rendered":"<h1 id=\"sql-server%e5%bb%ba%e7%ab%8b%e7%b2%be%e7%b7%bb%e7%9a%84%e8%b2%a8%e5%b9%a3%e5%9e%8b%e6%8f%9b%e7%ae%97%e7%b3%bb%e7%b5%b1-BOjkYUIfAg\">SQL Server\u5efa\u7acb\u7cbe\u7dfb\u7684\u8ca8\u5e63\u578b\u63db\u7b97\u7cfb\u7d71<\/h1>\n<p>\u5728\u7576\u4eca\u5168\u7403\u5316\u7684\u5546\u696d\u74b0\u5883\u4e2d\uff0c\u8ca8\u5e63\u578b\u63db\u7b97\u7cfb\u7d71\u7684\u9700\u6c42\u65e5\u76ca\u589e\u52a0\u3002\u7121\u8ad6\u662f\u96fb\u5b50\u5546\u52d9\u5e73\u53f0\u3001\u91d1\u878d\u61c9\u7528\u9084\u662f\u570b\u969b\u8cbf\u6613\uff0c\u6e96\u78ba\u7684\u8ca8\u5e63\u63db\u7b97\u90fd\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5728SQL Server\u4e2d\u5efa\u7acb\u4e00\u500b\u7cbe\u7dfb\u7684\u8ca8\u5e63\u578b\u63db\u7b97\u7cfb\u7d71\uff0c\u4e26\u63d0\u4f9b\u76f8\u95dc\u7684\u7bc4\u4f8b\u548c\u4ee3\u78bc\u3002<\/p>\n<h2 id=\"%e8%b2%a8%e5%b9%a3%e5%9e%8b%e6%95%b8%e6%93%9a%e9%a1%9e%e5%9e%8b%e7%9a%84%e9%81%b8%e6%93%87-BOjkYUIfAg\">\u8ca8\u5e63\u578b\u6578\u64da\u985e\u578b\u7684\u9078\u64c7<\/h2>\n<p>\u5728SQL Server\u4e2d\uff0c\u8ca8\u5e63\u578b\u6578\u64da\u901a\u5e38\u4f7f\u7528<strong>DECIMAL<\/strong>\u6216<strong>MONEY<\/strong>\u6578\u64da\u985e\u578b\u4f86\u5b58\u5132\u3002\u9019\u5169\u7a2e\u6578\u64da\u985e\u578b\u5404\u6709\u512a\u7f3a\u9ede\uff1a<\/p>\n<ul>\n<li><strong>MONEY<\/strong>\uff1a\u9069\u5408\u5b58\u5132\u91d1\u984d\uff0c\u7bc4\u570d\u5f9e-2,147,483,648.00\u52302,147,483,647.00\uff0c\u7cbe\u5ea6\u70ba\u56db\u4f4d\u5c0f\u6578\u3002<\/li>\n<li><strong>DECIMAL(p, s)<\/strong>\uff1a\u53ef\u4ee5\u81ea\u5b9a\u7fa9\u7cbe\u5ea6\u548c\u5c0f\u6578\u4f4d\u6578\uff0c\u9069\u5408\u9700\u8981\u9ad8\u7cbe\u5ea6\u7684\u91d1\u878d\u8a08\u7b97\u3002<\/li>\n<\/ul>\n<p>\u6839\u64da\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u985e\u578b\u662f\u5efa\u7acb\u63db\u7b97\u7cfb\u7d71\u7684\u7b2c\u4e00\u6b65\u3002<\/p>\n<h2 id=\"%e5%bb%ba%e7%ab%8b%e8%b2%a8%e5%b9%a3%e6%8f%9b%e7%ae%97%e8%a1%a8-BOjkYUIfAg\">\u5efa\u7acb\u8ca8\u5e63\u63db\u7b97\u8868<\/h2>\n<p>\u70ba\u4e86\u9032\u884c\u8ca8\u5e63\u63db\u7b97\uff0c\u6211\u5011\u9700\u8981\u4e00\u500b\u5305\u542b\u5404\u7a2e\u8ca8\u5e63\u53ca\u5176\u532f\u7387\u7684\u8868\u3002\u4ee5\u4e0b\u662f\u5efa\u7acb\u4e00\u500b\u7c21\u55ae\u7684\u8ca8\u5e63\u8868\u7684SQL\u8a9e\u53e5\uff1a<\/p>\n<pre><code>\nCREATE TABLE CurrencyExchangeRates (\n    CurrencyCode VARCHAR(3) PRIMARY KEY,\n    ExchangeRate DECIMAL(18, 4) NOT NULL,\n    LastUpdated DATETIME NOT NULL\n);\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u8868\u4e2d\uff0c<strong>CurrencyCode<\/strong>\u662f\u8ca8\u5e63\u7684\u4ee3\u78bc\uff08\u4f8b\u5982\uff1aUSD\u3001EUR\uff09\uff0c<strong>ExchangeRate<\/strong>\u662f\u76f8\u5c0d\u65bc\u57fa\u6e96\u8ca8\u5e63\u7684\u532f\u7387\uff0c<strong>LastUpdated<\/strong>\u5247\u8a18\u9304\u532f\u7387\u7684\u6700\u5f8c\u66f4\u65b0\u6642\u9593\u3002<\/p>\n<h2 id=\"%e6%8f%92%e5%85%a5%e5%8c%af%e7%8e%87%e6%95%b8%e6%93%9a-BOjkYUIfAg\">\u63d2\u5165\u532f\u7387\u6578\u64da<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u9700\u8981\u63d2\u5165\u4e00\u4e9b\u521d\u59cb\u7684\u532f\u7387\u6578\u64da\u3002\u4ee5\u4e0b\u662f\u63d2\u5165\u6578\u64da\u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>\nINSERT INTO CurrencyExchangeRates (CurrencyCode, ExchangeRate, LastUpdated)\nVALUES \n('USD', 1.0000, GETDATE()),\n('EUR', 0.8500, GETDATE()),\n('JPY', 110.0000, GETDATE());\n<\/code><\/pre>\n<h2 id=\"%e5%bb%ba%e7%ab%8b%e8%b2%a8%e5%b9%a3%e6%8f%9b%e7%ae%97%e5%87%bd%e6%95%b8-BOjkYUIfAg\">\u5efa\u7acb\u8ca8\u5e63\u63db\u7b97\u51fd\u6578<\/h2>\n<p>\u70ba\u4e86\u65b9\u4fbf\u9032\u884c\u8ca8\u5e63\u63db\u7b97\uff0c\u6211\u5011\u53ef\u4ee5\u5efa\u7acb\u4e00\u500b\u81ea\u5b9a\u7fa9\u51fd\u6578\u3002\u4ee5\u4e0b\u662f\u5be6\u73fe\u8ca8\u5e63\u63db\u7b97\u7684SQL\u51fd\u6578\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>\nCREATE FUNCTION dbo.ConvertCurrency\n(\n    @Amount DECIMAL(18, 4),\n    @FromCurrency VARCHAR(3),\n    @ToCurrency VARCHAR(3)\n)\nRETURNS DECIMAL(18, 4)\nAS\nBEGIN\n    DECLARE @FromRate DECIMAL(18, 4);\n    DECLARE @ToRate DECIMAL(18, 4);\n    \n    SELECT @FromRate = ExchangeRate FROM CurrencyExchangeRates WHERE CurrencyCode = @FromCurrency;\n    SELECT @ToRate = ExchangeRate FROM CurrencyExchangeRates WHERE CurrencyCode = @ToCurrency;\n    \n    RETURN (@Amount \/ @FromRate) * @ToRate;\nEND;\n<\/code><\/pre>\n<p>\u9019\u500b\u51fd\u6578\u63a5\u53d7\u4e09\u500b\u53c3\u6578\uff1a\u91d1\u984d\u3001\u4f86\u6e90\u8ca8\u5e63\u548c\u76ee\u6a19\u8ca8\u5e63\uff0c\u4e26\u8fd4\u56de\u63db\u7b97\u5f8c\u7684\u91d1\u984d\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8%e7%af%84%e4%be%8b-BOjkYUIfAg\">\u4f7f\u7528\u7bc4\u4f8b<\/h2>\n<p>\u4f7f\u7528\u6211\u5011\u525b\u525b\u5efa\u7acb\u7684\u51fd\u6578\u9032\u884c\u8ca8\u5e63\u63db\u7b97\u975e\u5e38\u7c21\u55ae\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>\nDECLARE @ConvertedAmount DECIMAL(18, 4);\nSET @ConvertedAmount = dbo.ConvertCurrency(100.00, 'USD', 'EUR');\nSELECT @ConvertedAmount AS ConvertedAmount;\n<\/code><\/pre>\n<p>\u9019\u6bb5\u4ee3\u78bc\u5c07100\u7f8e\u5143\u63db\u7b97\u70ba\u6b50\u5143\uff0c\u4e26\u8fd4\u56de\u63db\u7b97\u5f8c\u7684\u91d1\u984d\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-BOjkYUIfAg\">\u7e3d\u7d50<\/h2>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u63a2\u8a0e\u4e86\u5982\u4f55\u5728SQL Server\u4e2d\u5efa\u7acb\u4e00\u500b\u7cbe\u7dfb\u7684\u8ca8\u5e63\u578b\u63db\u7b97\u7cfb\u7d71\uff0c\u5305\u62ec\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u985e\u578b\u3001\u5efa\u7acb\u532f\u7387\u8868\u3001\u63d2\u5165\u6578\u64da\u4ee5\u53ca\u5275\u5efa\u63db\u7b97\u51fd\u6578\u3002\u9019\u4e9b\u6b65\u9a5f\u5c07\u5e6b\u52a9\u958b\u767c\u8005\u5728\u5176\u61c9\u7528\u4e2d\u5be6\u73fe\u6e96\u78ba\u7684\u8ca8\u5e63\u63db\u7b97\u529f\u80fd\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u60a8\u7684\u61c9\u7528\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a> \u670d\u52d9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5efa\u7acb\u7cbe\u7dfb\u7684SQL Server\u8ca8\u5e63\u578b\u63db\u7b97\u7cfb\u7d71\uff0c\u8f15\u9b06\u7ba1\u7406\u591a\u7a2e\u8ca8\u5e63\u8f49\u63db\uff0c\u63d0\u5347\u8ca1\u52d9\u6578\u64da\u8655\u7406\u6548\u7387\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-138907","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/138907","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=138907"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/138907\/revisions"}],"predecessor-version":[{"id":138908,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/138907\/revisions\/138908"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=138907"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=138907"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=138907"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}