对所有表进行类似处理

来源:互联网 发布:mac sass安装出错 编辑:程序博客网 时间:2024/05/24 02:38
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

eg1:

大家知道,如果将一个表所有者改为dbo,只需

sp_changeobjectowner'表名','dbo'即可

但要将所有的表的所有者都改为dbo,可以用循环处理,此处略

其实用SQL的系统存储过程sp_MSForEachTable可以轻松搞定

只需execsp_MSForEachTable'sp_changeobjectowner''?'',''dbo'''

当然,可能运行时会有错(比如有些表的所有者原先就是dbo),不用理它,运行一遍就行了。

 

eg2:

所有表都加上一个自增长的字段

大家知道对一个表加的话只需:altertable表名addiidintidentity(1,1)

若需对所有的表都加的话只需:sp_MSForEachTable'altertable?addiidintidentity(1,1)'

同理,删除所有表中的iid字段只需sp_MSForEachTable'altertable?dropcolumniid'


当然,可能运行时会有错(比如加字段时该表中已有该字段,删除字段时没有该字段),不用理它,运行一遍就行了。

 

eg3:

对所有表中的num字段进行四舍五入处理

sp_MSForEachTable'update?setnum=round(num,2)'

当然,可能运行时会有错(比如有些表中没有num字段),不用理它,运行一遍就行了。
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>