{"id":622,"date":"2010-08-20T10:44:23","date_gmt":"2010-08-20T02:44:23","guid":{"rendered":""},"modified":"2014-03-11T22:32:58","modified_gmt":"2014-03-11T14:32:58","slug":"sql2000-%e4%bf%ae%e6%94%b9%e8%a1%a8%e6%89%80%e6%9c%89%e8%80%85%e7%9a%84%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"http:\/\/www.xiaoyebailong.com\/index.php\/2010\/08\/20\/622.htm","title":{"rendered":"SQL2000 \u4fee\u6539\u8868\u6240\u6709\u8005\u7684\u65b9\u6cd5"},"content":{"rendered":"<p><span class=\"oblog_text\"> <\/span><\/p>\n<p>\u5355\u4e2a\u4fee\u6539\u6240\u6709\u8005sql\u8bed\u53e5\u5982\u4e0b\uff1a<\/p>\n<p>\u67e5\u8be2\u5206\u6790\u5668\u8f93\u5165\uff1aEXEC sp_changeobjectowner &#8216;user.table&#8217;, &#8216;dbo&#8217;<\/p>\n<p>user.table\u7684\u610f\u601d\u4e3a\uff1a\u6240\u6709\u8005.\u8868\u540d\uff0c\u6bd4\u5982oblog.oblog_user\uff0c\u4ee5\u524d\u7684\u8bed\u53e5\u8868\u793a\u5c06table\u8fd9\u5f20\u8868\u7684\u6240\u6709\u8005\u7531<\/p>\n<p>user\u6539\u6210dbo<\/p>\n<p>\u6279\u91cf\u4fee\u6539\u6240\u6709\u8005\u8bed\u53e5\u5982\u4e0b\uff1a<\/p>\n<p>\u67e5\u8be2\u5206\u6790\u5668\u8f93\u5165\uff1aexec sp_msforeachtable &#8216;sp_changeobjectowner &#8221;?&#8221;, &#8221;dbo&#8221;&#8217;<\/p>\n<p>\u5373\u53ef\u628a\u5f53\u9762\u8868\u7684\u6240\u6709\u6240\u6709\u8005\u6362\u6210dbo<\/p>\n<p>\u7b2c\u4e8c\u79cd\u65b9\u6cd5\uff1a<\/p>\n<p>\u66f4\u6539\u6570\u636e\u5e93\u6240\u6709\u8005\u5e76\u4e0d\u80fd\u5f71\u54cd\u5df2\u7ecf\u521b\u5efa\u7684\u8868\u7684\u6240\u6709\u8005\uff0c\u4f60\u9700\u8981\u4f7f\u7528sp_changeobjectower\u6765\u6539\u53d8\u5bf9\u8c61\u7684\u6240\u6709\u8005\uff0c\u5e76\u4e14\u5bf9\u8c61\u7684\u6240\u6709\u8005sa\u5bf9\u5e94\u7684\u662fdbo<\/p>\n<p>\u4ee5\u4e0b\u8bed\u53e5\u662f\u5229\u7528\u6e38\u6807\uff0c\u8bfb\u53d6mydb\u5e10\u6237\u62e5\u6709\u7684\u6240\u6709\u5bf9\u8c61\uff0c\u5e76\u5c06\u5176\u6240\u6709\u8005\u66f4\u6539\u4e3asa <br \/>\n\u53e6\u5916\u5982\u679c\u4e4b\u524d\u672c\u5730\u4e0d\u5b58\u5728mydb\u5e10\u6237\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5b64\u7acb\u5e10\u6237\u7684\u51fa\u73b0\uff0c\u4f60\u5148\u8bd5\u8bd5\u80fd\u4e0d\u80fd\u6210\u529f\u5427\uff0c\u5982\u679c\u4e0d\u884c\uff0c\u518d\u63d0\u4f9b\u7ed9\u4f60\u89e3\u51b3\u65b9\u6cd5\u3002<\/p>\n<p>declare cur cursor for <br \/>\nselect name <br \/>\nfrom sysobjects <br \/>\nwhere uid=user_id(&#8216;mydb&#8217;)<\/p>\n<p>declare @name sysname <br \/>\ndeclare @sqlstr varchar(5000)<\/p>\n<p>open cur <br \/>\nfetch next from cur <br \/>\ninto @name<\/p>\n<p>while @@fetch_status=0 <br \/>\nbegin <br \/>\nset @sqlstr=&#8217;exec sp_changeobjectowner &#8221;mydb.&#8217;+@name+&#8221;&#8217;,&#8221;dbo&#8221;&#8217; <br \/>\nexec (@sqlstr) <br \/>\nfetch next from cur <br \/>\ninto @name <br \/>\nend <br \/>\nclose cur <br \/>\ndeallocate cur<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u6279\u91cf\u4fee\u6539\u50a8\u5b58\u8fc7\u7a0b\u6240\u6709\u8005\u7684\u65b9\u5f0f<br \/>\n<\/strong>\u7b2c\u4e00\u6b65,\u5148\u901a\u8fc7\u67e5\u8be2\u5206\u6790\u5668\u5efa\u7acb\u50a8\u5b58\u8fc7\u7a0b,\u4ee3\u7801\u5982\u4e0b<\/p>\n<p>CREATE&nbsp;&nbsp; PROCEDURE&nbsp;&nbsp; ChangeProcOwner&nbsp;&nbsp; <br \/>\n@OldOwner as&nbsp;&nbsp; NVARCHAR(128),&#8211;\u53c2\u6570\u539f\u6240\u6709\u8005&nbsp;&nbsp; <br \/>\n@NewOwner as&nbsp;&nbsp; NVARCHAR(128)&#8211;\u53c2\u6570\u65b0\u6240\u6709\u8005&nbsp;&nbsp; <br \/>\nAS&nbsp;&nbsp; <br \/>\n&nbsp;&nbsp;&nbsp; <br \/>\nDECLARE&nbsp;&nbsp; @Name&nbsp;&nbsp; as&nbsp;&nbsp; NVARCHAR(128)&nbsp;&nbsp;&nbsp;&nbsp; <br \/>\nDECLARE&nbsp;&nbsp; @Owner as&nbsp;&nbsp; NVARCHAR(128)&nbsp;&nbsp; <br \/>\nDECLARE&nbsp;&nbsp; @OwnerName as&nbsp;&nbsp; NVARCHAR(128)&nbsp;&nbsp; <br \/>\n&nbsp;&nbsp;&nbsp; <br \/>\nDECLARE&nbsp;&nbsp; curObject&nbsp;&nbsp; CURSOR&nbsp;&nbsp; FOR&nbsp;&nbsp;&nbsp;&nbsp; <br \/>\nselect &#8216;Name&#8217; =&nbsp;&nbsp; name,&nbsp;&nbsp; <br \/>\n&#8216;Owner&#8217; =&nbsp;&nbsp; user_name(uid)&nbsp;&nbsp; <br \/>\nfrom&nbsp;&nbsp; sysobjects&nbsp;&nbsp; <br \/>\nwhere&nbsp;&nbsp; user_name(uid)=@OldOwner&nbsp;&nbsp; and&nbsp;&nbsp; xtype=&#8217;p&#8217;&nbsp;&nbsp; <br \/>\norder&nbsp;&nbsp; by&nbsp;&nbsp; name&nbsp;&nbsp; <br \/>\n&nbsp;&nbsp;&nbsp; <br \/>\nOPEN&nbsp;&nbsp;&nbsp;&nbsp; curObject&nbsp;&nbsp; <br \/>\nFETCH&nbsp;&nbsp; NEXT&nbsp;&nbsp; FROM&nbsp;&nbsp; curObject&nbsp;&nbsp; INTO&nbsp;&nbsp; @Name,&nbsp;&nbsp; @Owner&nbsp;&nbsp; <br \/>\nWHILE(@@FETCH_STATUS=0)&nbsp;&nbsp; <br \/>\nBEGIN&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br \/>\nif&nbsp;&nbsp; @Owner=@OldOwner&nbsp;&nbsp;&nbsp;&nbsp; <br \/>\nbegin&nbsp;&nbsp; <br \/>\nset&nbsp;&nbsp; @OwnerName&nbsp;&nbsp; =&nbsp;&nbsp; @OldOwner&nbsp;&nbsp; +&nbsp;&nbsp; &#8216;.&#8217;&nbsp;&nbsp; +&nbsp;&nbsp; rtrim(@Name)&nbsp;&nbsp; <br \/>\nexec&nbsp;&nbsp; sp_changeobjectowner&nbsp;&nbsp; @OwnerName,&nbsp;&nbsp; @NewOwner&nbsp;&nbsp; <br \/>\nend&nbsp;&nbsp; <br \/>\n&nbsp;&nbsp;&nbsp; <br \/>\nFETCH&nbsp;&nbsp; NEXT&nbsp;&nbsp; FROM&nbsp;&nbsp; curObject&nbsp;&nbsp; INTO&nbsp;&nbsp; @Name,&nbsp;&nbsp; @Owner&nbsp;&nbsp; <br \/>\nEND&nbsp;&nbsp; <br \/>\n&nbsp;&nbsp;&nbsp; <br \/>\nclose&nbsp;&nbsp; curObject&nbsp;&nbsp; <br \/>\ndeallocate&nbsp;&nbsp; curObject&nbsp;&nbsp; <br \/>\nGO<\/p>\n<p>\u7b2c\u4e8c\u6b65:\u7136\u540e\u5728\u67e5\u8be2\u5206\u6790\u5668\u91cc\u7ee7\u7eed\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c<\/p>\n<p>exec&nbsp;&nbsp; ChangeProcOwner&nbsp;&nbsp;&nbsp; &#8216;\u539f\u6240\u6709\u8005&#8217;,&#8217;dbo&#8217;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5355\u4e2a\u4fee\u6539\u6240\u6709\u8005sql\u8bed\u53e5\u5982\u4e0b\uff1a \u67e5\u8be2\u5206\u6790\u5668\u8f93\u5165\uff1aEXEC sp_changeobjectowner &#8216;user.table&#8217;, &#8216;dbo&#8217; user.table\u7684\u610f\u601d\u4e3a\uff1a\u6240\u6709\u8005.\u8868\u540d\uff0c\u6bd4\u5982ob&#8230;<\/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":[],"topic":[],"class_list":["post-622","post","type-post","status-publish","format-standard","hentry","category-technology"],"_links":{"self":[{"href":"http:\/\/www.xiaoyebailong.com\/index.php\/wp-json\/wp\/v2\/posts\/622","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.xiaoyebailong.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.xiaoyebailong.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.xiaoyebailong.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.xiaoyebailong.com\/index.php\/wp-json\/wp\/v2\/comments?post=622"}],"version-history":[{"count":1,"href":"http:\/\/www.xiaoyebailong.com\/index.php\/wp-json\/wp\/v2\/posts\/622\/revisions"}],"predecessor-version":[{"id":66466,"href":"http:\/\/www.xiaoyebailong.com\/index.php\/wp-json\/wp\/v2\/posts\/622\/revisions\/66466"}],"wp:attachment":[{"href":"http:\/\/www.xiaoyebailong.com\/index.php\/wp-json\/wp\/v2\/media?parent=622"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.xiaoyebailong.com\/index.php\/wp-json\/wp\/v2\/categories?post=622"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.xiaoyebailong.com\/index.php\/wp-json\/wp\/v2\/tags?post=622"},{"taxonomy":"topic","embeddable":true,"href":"http:\/\/www.xiaoyebailong.com\/index.php\/wp-json\/wp\/v2\/topic?post=622"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}