{"id":2201,"date":"2020-05-05T18:16:17","date_gmt":"2020-05-05T09:16:17","guid":{"rendered":"https:\/\/www.dbsheetclient.jp\/blog\/?p=2201"},"modified":"2020-05-05T18:37:18","modified_gmt":"2020-05-05T09:37:18","slug":"%e3%83%87%e3%83%bc%e3%82%bf%e3%83%99%e3%83%bc%e3%82%b9%e3%83%88%e3%83%aa%e3%82%ac%e3%83%bc%ef%bc%88sql-server%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.dbsheetclient.jp\/blog\/?p=2201","title":{"rendered":"\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30c8\u30ea\u30ac\u30fc\uff08SQL Server\uff09"},"content":{"rendered":"<p>IT\u30b3\u30fc\u30c7\u30a3\u30cd\u30fc\u30bf\u306e\u30b7\u30e5\u30a6\u3067\u3059\u3002<\/p>\n<p><a href=\"https:\/\/www.dbsheetclient.jp\/blog\/wp-content\/uploads\/2020\/05\/IMG_2842.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-2202\" src=\"https:\/\/www.dbsheetclient.jp\/blog\/wp-content\/uploads\/2020\/05\/IMG_2842-300x225.jpg\" alt=\"IMG_2842\" width=\"300\" height=\"225\" srcset=\"https:\/\/www.dbsheetclient.jp\/blog\/wp-content\/uploads\/2020\/05\/IMG_2842-300x225.jpg 300w, https:\/\/www.dbsheetclient.jp\/blog\/wp-content\/uploads\/2020\/05\/IMG_2842-1024x768.jpg 1024w, https:\/\/www.dbsheetclient.jp\/blog\/wp-content\/uploads\/2020\/05\/IMG_2842.jpg 2016w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>\u304a\u4e45\u3057\u3076\u308a\u3067\u3059\u3002\u5199\u771f\u306f4\u6708\u306e\u521d\u3081\u9803\u306b\u64ae\u3063\u305f\u685c\u306e\u5199\u771f\u3067\u3059\u3002\u65e9\u304f\u30d6\u30ed\u30b0\u306b\u30a2\u30c3\u30d7\u3057\u3088\u3046\u3068\u601d\u3044\u306a\u304c\u3089\u30014\u6708\u304b\u3089\u65b0\u3057\u304f\u95a2\u308f\u3063\u305f\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306e\u307b\u3046\u3082\u5fd9\u3057\u304f\u30011\u304b\u6708\u304c\u3042\u3063\u3068\u3044\u3046\u9593\u306b\u904e\u304e\u3066\u3057\u307e\u3044\u307e\u3057\u305f\u3002<br \/>\u3055\u3066\u3001\u4eca\u5e74\u306e\u521d\u3081\u306b\u306f\u4e88\u60f3\u3082\u3057\u3066\u3044\u306a\u304b\u3063\u305f\u30b3\u30ed\u30ca\u30a6\u30a3\u30eb\u30b9\u306e\u5f71\u97ff\u3067\u3001\u4e16\u754c\u304c\u5168\u304f\u9055\u3046\u3082\u306e\u306b\u306a\u3063\u305f\u3088\u3046\u306a\u73fe\u72b6\u306b\u3001\u672c\u5f53\u306b\u9a5a\u304d\u6238\u60d1\u3046\u3053\u3068\u304c\u591a\u3044\u6bce\u65e5\u3067\u3059\u3002\u30a6\u30a3\u30eb\u30b9\u3068\u306e\u95d8\u3044\u306e\u6700\u5148\u7aef\u3067\u82e6\u52b4\u3055\u308c\u3066\u3044\u308b\u533b\u7642\u5f93\u4e8b\u8005\u306e\u65b9\u306b\u306f\u672c\u5f53\u306b\u611f\u8b1d\u3092\u3057\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3068\u601d\u3044\u307e\u3059\u3002\u30b4\u30fc\u30eb\u30c7\u30f3\u30a6\u30a4\u30fc\u30af\u3082\u307b\u307c\u81ea\u5b85\u306b\u3044\u308b\u3068\u3044\u3046\u72b6\u6cc1\u3067\u3001\u6620\u753b\u306eDVD\u3092\u501f\u308a\u3066\u304d\u3066\u5bb6\u65cf\u3067\u898b\u308b\u3068\u304b\u3001\u305d\u3093\u306a\u904e\u3054\u3057\u65b9\u3092\u305b\u3056\u308b\u3092\u5f97\u306a\u3044\u72b6\u6cc1\u3067\u306f\u3042\u308a\u307e\u3059\u304c\u3001\u56fd\u6c11\u304c\u4e00\u3064\u3068\u306a\u3063\u3066\u3053\u306e\u3068\u304d\u3092\u4e57\u308a\u8d8a\u3048\u3066\u3044\u3051\u308b\u3088\u3046\u3001\u305d\u3057\u30661\u65e5\u3082\u65e9\u304f\u7d42\u606f\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3088\u3046\u306b\u5fc3\u304b\u3089\u304a\u7948\u308a\u3044\u305f\u3057\u307e\u3059\u3002<br \/><strong><br \/>\uff1c\u672c\u65e5\u306e\u984c\u6750\uff1e<br \/><\/strong><strong>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30c8\u30ea\u30ac\u30fc\uff08SQL Server\uff09<br \/><\/strong><br \/>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30c8\u30ea\u30ac\u30fc\u306b\u3064\u3044\u3066\u3001\u4ee5\u524d\u3001Oracle\u3067\u53d6\u308a\u4e0a\u3052\u307e\u3057\u305f\u304c\u3001\u4eca\u56de\u306fSQL Server\u306b\u3066\u540c\u69d8\u306e\u8a2d\u5b9a\u306b\u3064\u3044\u3066\u78ba\u8a8d\u3057\u3066\u307f\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<br \/>SQL Server\u30c8\u30ea\u30ac\u30fc\u306b\u306f\u3001\u5927\u304d\u304f\u306f\u300c\u30ed\u30b0\u30aa\u30f3\u30c8\u30ea\u30ac\u30fc\u300d\u300cDDL\u30c8\u30ea\u30ac\u30fc\u300d\u300cDML\u30c8\u30ea\u30ac\u30fc\u300d\u306e3\u3064\u306e\u7a2e\u985e\u304c\u3042\u308a\u3001\u305d\u308c\u305e\u308c\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u5185\u5bb9\u306b\u306a\u308a\u307e\u3059\u3002<br \/>\u300c\u30ed\u30b0\u30aa\u30f3\u30c8\u30ea\u30ac\u30fc\u300d\u306f\u3001\u30ed\u30b0\u30aa\u30f3\u6642\u30e6\u30fc\u30b6\u30fc\u30bb\u30c3\u30b7\u30e7\u30f3\u304c\u78ba\u7acb\u3055\u308c\u308b\u3068\u304d\u306b\u5b9f\u884c\u3059\u308b\u3082\u306e<br \/>\u300cDDL\u30c8\u30ea\u30ac\u30fc\u300d\u306f\u3001DDL\u64cd\u4f5c(CREATE, ALTER, DROP\u306a\u3069)\u306b\u3088\u308a\u8d77\u52d5\u3059\u308b\u3082\u306e<br \/>\u300cDML\u30c8\u30ea\u30ac\u30fc\u300d\u306f\u3001\u7279\u5b9a\u306e\u8868\u3084\u30d3\u30e5\u30fc\u306b\u5bfe\u3057\u3066DML\u64cd\u4f5c(INSERT, UPDATE, DELETE\u306a\u3069)\u304c\u884c\u308f\u308c\u308b\u3068\u304d\u306b\u8d77\u52d5\u3059\u308b\u3082\u306e<\/p>\n<p>\u524d\u56de\u3001Oracle\u3067\u300cDML\u30c8\u30ea\u30ac\u30fc\u300d\u306e\u300cAFTER\u30c8\u30ea\u30ac\u30fc\u300d\u3092\u4f7f\u3063\u305f\u4f8b\u3092\u7d39\u4ecb\u3057\u307e\u3057\u305f\u304c\u3001\u4eca\u56de\u306fSQLServer\u3067\u540c\u69d8\u306e\u8a2d\u5b9a\u3092\u3057\u3066\u307f\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u4f8b\uff09<br \/>\u524d\u56de\u540c\u69d8\u3001\u30e6\u30fc\u30b6\u30fc\u7ba1\u7406\u7528\u306e\u300cACCOUNTS\u300d\u30c6\u30fc\u30d6\u30eb\u3068\u3044\u3046\u3082\u306e\u304c\u3042\u3063\u305f\u3068\u3057\u3066\u3001\u305d\u308c\u306b\u5bfe\u3057\u3066\u306e\u3001INSERT, UPDATE, DELETE \u304c\u884c\u308f\u308c\u308b\u30bf\u30a4\u30df\u30f3\u30b0(AFTER)\u3067\u3001\u51e6\u7406\u306e\u30ed\u30b0\u3092\u300cACCOUNTS_LOG\u300d\u30c6\u30fc\u30d6\u30eb\u306b\u767b\u9332\u3059\u308b\u30c8\u30ea\u30ac\u30fc\u3092\u4f5c\u6210\u3057\u3066\u307f\u307e\u3059\u3002 <br \/>\u307e\u305a\u3001\u51e6\u7406\u306e\u30ed\u30b0\u3092\u767b\u9332\u3059\u308b\u300cACCOUNTS_LOG\u300d\u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n<p>CREATE TABLE accounts_log(<br \/>log_date DATE,<br \/>old_account_id INT,<br \/>new_account_id INT,<br \/>old_name NVARCHAR(20),<br \/>new_name NVARCHAR(20),<br \/>action NVARCHAR(20));<\/p>\n<p>\u6b21\u306b\u3001\u30c8\u30ea\u30ac\u30fc\u3092\u4f5c\u6210\u3057\u307e\u3059\u304c\u3001\u300cACCOUNTS\u300d\u30c6\u30fc\u30d6\u30eb\u306b\u5bfe\u3057\u3066\u306e\u3001INSERT, UPDATE, DELETE \u304c\u884c\u308f\u308c\u3066\u30c7\u30fc\u30bf\u304c\u66f4\u65b0\u3055\u308c\u308b\u305f\u3073\u306b\u3001\u300cACCOUNTS_LOG\u300d\u30c6\u30fc\u30d6\u30eb\u306b\u3001\u51e6\u7406\u65e5\u3001\u5909\u66f4\u524d\u5f8c\u306e\u300cACCOUNT_ID\u300d\u3001\u300cNAME\u300d\u305d\u3057\u3066\u51e6\u7406\u5185\u5bb9\u3092\u8a18\u9332\u3055\u305b\u307e\u3059\u3002<\/p>\n<p>&#8211;INSERT\u3001DELETE\u306e\u5c65\u6b74\u767b\u9332\u7528<br \/>CREATE TRIGGER accounts_ins_del_trigger<br \/>ON accounts<br \/>AFTER INSERT, DELETE<br \/>AS<br \/>BEGIN<br \/>INSERT INTO accounts_log (log_date, old_account_id, new_account_id, old_name, new_name, action)<br \/>SELECT GETDATE(), NULL, account_id, NULL, name, &#8216;INSERT&#8217;<br \/>FROM inserted<br \/>UNION<br \/>SELECT GETDATE(), account_id, NULL, name, NULL, &#8216;DELETE&#8217;<br \/>FROM deleted; <br \/>END;<br \/>GO<\/p>\n<p>&#8211;UPDATE\u306e\u5c65\u6b74\u767b\u9332\u7528<br \/>CREATE TRIGGER accounts_upd_trigger<br \/>ON accounts<br \/>AFTER UPDATE<br \/>AS<br \/>BEGIN<br \/>INSERT INTO accounts_log (log_date, old_account_id, new_account_id, old_name, new_name, action)<br \/>SELECT GETDATE(), account_id, NULL, name, NULL, &#8216;UPDATE&#8217;<br \/>FROM deleted<br \/>UNION<br \/>SELECT GETDATE(), NULL, account_id, NULL, name, &#8216;UPDATE&#8217;<br \/>FROM inserted<br \/>END;<br \/>GO<\/p>\n<p>\u524d\u56de\u540c\u69d8\u4e0b\u8a18\u306e\u3088\u3046\u306b\u3001\u300cACCOUNTS\u300d\u30c6\u30fc\u30d6\u30eb\u3078\u306e\u30c7\u30fc\u30bf\u306e\u767b\u9332\u3001\u4fee\u6b63\u3001\u524a\u9664\u3092\u9806\u306b\u884c\u3063\u3066\u307f\u307e\u3059\u3002<\/p>\n<p>INSERT INTO accounts(account_id, name) VALUES(101, &#8216;\u5c71\u7530\u3000\u592a\u90ce&#8217;);<br \/>INSERT INTO accounts(account_id, name) VALUES(102, &#8216;\u9ad8\u6a4b\u3000\u4e09\u90ce&#8217;);<br \/>UPDATE accounts SET NAME = &#8216;\u9ad8\u6a4b\u3000\u4e00\u90ce&#8217; WHERE account_id = 102;<br \/>DELETE FROM accounts WHERE account_id = 101;<\/p>\n<p>\u305d\u308c\u3067\u306f\u3001\u300cACCOUNTS_LOG\u300d\u30c6\u30fc\u30d6\u30eb\u3092\u78ba\u8a8d\u3057\u3066\u307f\u307e\u3059\u3002<br \/>SELECT * FROM accounts_log<br \/>ORDER BY log_date;<\/p>\n<p><a href=\"https:\/\/www.dbsheetclient.jp\/blog\/wp-content\/uploads\/2020\/05\/accounts_log.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-2206\" src=\"https:\/\/www.dbsheetclient.jp\/blog\/wp-content\/uploads\/2020\/05\/accounts_log-300x130.png\" alt=\"accounts_log\" width=\"300\" height=\"130\" srcset=\"https:\/\/www.dbsheetclient.jp\/blog\/wp-content\/uploads\/2020\/05\/accounts_log-300x130.png 300w, https:\/\/www.dbsheetclient.jp\/blog\/wp-content\/uploads\/2020\/05\/accounts_log.png 672w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p><\/a>\u305f\u3057\u304b\u306b\u3001\u300cACCOUNTS_LOG\u300d\u30c6\u30fc\u30d6\u30eb\u3078\u306e\u66f4\u65b0\u5c65\u6b74\u304c\u767b\u9332\u3055\u308c\u3066\u3044\u307e\u3059<a href=\"https:\/\/www.dbsheetclient.jp\/blog\/wp-content\/uploads\/2020\/05\/accounts_log.png\">\u3002<\/p>\n<p><\/a>\u3053\u3053\u3067\u3001SQL Server\u306e\u5834\u5408\u3001DML \u30c8\u30ea\u30ac\u30fc\u30b9\u30c6\u30fc\u30c8\u30e1\u30f3\u30c8\u3067\u306f\u3001deleted \u30c6\u30fc\u30d6\u30eb\u304a\u3088\u3073 inserted \u30c6\u30fc\u30d6\u30eb\u3068\u3044\u3046 2 \u3064\u306e\u7279\u6b8a\u306a\u30c6\u30fc\u30d6\u30eb\u304c\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002\u305d\u3057\u3066\u3001deleted \u30c6\u30fc\u30d6\u30eb\u306b\u306f\u3001DELETE \u30b9\u30c6\u30fc\u30c8\u30e1\u30f3\u30c8\u3068 UPDATE \u30b9\u30c6\u30fc\u30c8\u30e1\u30f3\u30c8\u306e\u5b9f\u884c\u3067\u5f71\u97ff\u3092\u53d7\u3051\u305f\u884c\u306e\u30b3\u30d4\u30fc\u304c\u683c\u7d0d\u3055\u308c\u3001inserted \u30c6\u30fc\u30d6\u30eb\u306b\u306f\u3001INSERT \u30b9\u30c6\u30fc\u30c8\u30e1\u30f3\u30c8\u304a\u3088\u3073 UPDATE \u30b9\u30c6\u30fc\u30c8\u30e1\u30f3\u30c8\u306e\u5b9f\u884c\u3067\u5f71\u97ff\u3092\u53d7\u3051\u305f\u884c\u306e\u30b3\u30d4\u30fc\u304c\u683c\u7d0d\u3055\u308c\u308b\u304b\u305f\u3061\u306b\u306a\u308a\u307e\u3059\u3002\u00a0\u3064\u307e\u308a\u3001UPDATE\u3068\u3044\u3046\u66f4\u65b0\u51e6\u7406\u306f\u3001DELETE\u306e\u524a\u9664\u51e6\u7406\u3068\u305d\u308c\u306b\u7d9a\u304fINSERT\u306e\u633f\u5165\u51e6\u7406\u306e\u7d44\u307f\u5408\u308f\u305b\u3068\u8003\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u3001deleted \u30c6\u30fc\u30d6\u30eb\u306b\u53e4\u3044\u884c\u304c\u30b3\u30d4\u30fc\u3055\u308c\u3001\u305d\u306e\u5f8c\u3001\u65b0\u3057\u3044\u884c\u304cinserted\u30c6\u30fc\u30d6\u30eb\u306b\u30b3\u30d4\u30fc\u3055\u308c\u308b\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u305d\u306e\u305f\u3081\u3001\u300cACCOUNTS_LOG\u300d\u30c6\u30fc\u30d6\u30eb\u3078\u306e\u66f4\u65b0\u5c65\u6b74\u306b\u306f\u3001UPDATE\u306b\u95a2\u3057\u3066\u306f\u3001from deleted\u3001from inserted \u304b\u3089\u30ed\u30b0\u3092\u53d6\u5f97\u3059\u308b\u3088\u3046\u306b\u30c8\u30ea\u30ac\u30fc\u3092\u4f5c\u3063\u3066\u3044\u307e\u3059\u3002\u66f4\u65b0\u5c65\u6b74\u306b\u3082\u3001UPDATE\u51e6\u7406\u306b\u3088\u308a\u524a\u9664\u3001\u767b\u9332\u306e2\u3064\u306e\u884c\u304c\u78ba\u8a8d\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u4eca\u65e5\u306f\u4ee5\u4e0a\u307e\u3067<\/p>\n<p><a href=\"https:\/\/www.dbsheetclient.jp\/blog\/wp-content\/uploads\/2020\/05\/accounts_log.png\"><br \/> <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>IT\u30b3\u30fc\u30c7\u30a3\u30cd\u30fc\u30bf\u306e\u30b7\u30e5\u30a6\u3067\u3059\u3002 \u304a\u4e45\u3057\u3076\u308a\u3067\u3059\u3002\u5199\u771f\u306f4\u6708\u306e\u521d\u3081\u9803\u306b\u64ae\u3063\u305f\u685c\u306e\u5199\u771f\u3067\u3059\u3002\u65e9\u304f\u30d6\u30ed\u30b0\u306b\u30a2\u30c3\u30d7\u3057\u3088\u3046\u3068\u601d\u3044\u306a\u304c\u3089\u30014\u6708\u304b\u3089\u65b0\u3057\u304f\u95a2\u308f\u3063\u305f\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306e\u307b\u3046\u3082\u5fd9\u3057\u304f\u30011\u304b\u6708\u304c\u3042\u3063\u3068\u3044\u3046\u9593\u306b\u904e\u304e\u3066\u3057\u307e\u3044\u307e\u3057\u305f\u3002 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[10],"tags":[],"class_list":["post-2201","post","type-post","status-publish","format-standard","hentry","category-10"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.dbsheetclient.jp\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2201","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dbsheetclient.jp\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dbsheetclient.jp\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dbsheetclient.jp\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dbsheetclient.jp\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2201"}],"version-history":[{"count":5,"href":"https:\/\/www.dbsheetclient.jp\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2201\/revisions"}],"predecessor-version":[{"id":2208,"href":"https:\/\/www.dbsheetclient.jp\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2201\/revisions\/2208"}],"wp:attachment":[{"href":"https:\/\/www.dbsheetclient.jp\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2201"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dbsheetclient.jp\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2201"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dbsheetclient.jp\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2201"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}