« 图象按比例 缩放自定义提示条 »

一个值得研究的系统存储过程 sp_MSforeachtable

它的应用:

---------------------------------------------------------------------

sp_MSforeachtable @command1='Delete from ?'
这样可以删除所有表中的数据

也可以这样写

sp_MSforeachtable @command1 = "TRUNCATE TABLE ?"

----------------------------------------------------------------------

创建一个新表AAA,表中包含其它表的名称和记录数量:

create table AAA(id int identity(1,1),tablename varchar(50),[rowcount] int)

delete from AAA

exec sp_msforeachtable 'insert AAA(tablename, [rowcount]) select N''?'', count(*) from ?'

select * from AAA where AAA.[rowcount]>0

----------------------------------------------------------------------

1。在sql server2000下一个数据库中有建立了几十个表,现在要给每个表增加同样的列。可以用"alter table"语句,但是这么多表一个一个的操作太麻烦,有什么方法可以操作循环每一个表呢?
方法:sp_msforeachtable 'alter table ? add 列名 int'



2。假设所有表中的'note' 字段不能为空,如果为空则要改为"。
方法:sp_msforeachtable 'update ? set note='''' where note is null'

----------------------------------------------------------------------

结论: 看了很多例子以后发现sp_msforeachtable 里面那个问号指代数据库中所有的表

此存储过程还有一个参数为@whereend,可以这么写 @whereend='name in 'table1','table2',... ...'

这个参数是限制此存储过程的操作用于哪些表

原创文章如转载,请注明:转载自悠悠博客 [ http://www.ajaxstu.com/ ]

相关文章:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。