一个值得研究的系统存储过程 sp_MSforeachtable
2006-10-09 20:44
471 查看
它的应用:
---------------------------------------------------------------------
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',... ...'
这个参数是限制此存储过程的操作用于哪些表
---------------------------------------------------------------------
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',... ...'
这个参数是限制此存储过程的操作用于哪些表
相关文章推荐
- 一个值得研究的系统存储过程 sp_MSforeachtable
- 系统存储过程sp_MSforeachtable的使用说明
- 系统存储过程sp_MSforeachtable和sp_MSforeachdb
- 系统存储过程sp_MSforeachtable和sp_MSforeachdb使用说明
- 系统存储过程sp_MSforeachtable和sp_MSforeachdb
- SQL Server 系统存储过程sp_MSforeachtable
- 详解SQL Server的两个存储过程:sp_MSforeachtable/sp_MSforeachdb
- 详解SQL Server的两个存储过程:sp_MSforeachtable/sp_MSforeachdb
- SQL SERVERR中未公开的存储过程sp_MSforeachtable
- (SqlServer)不公开存储过程sp_Msforeachtable与sp_Msforeachdb详解
- (SqlServer)不公开存储过程sp_Msforeachtable与sp_Msforeachdb详解
- 详解SQL Server的两个存储过程:sp_MSforeachtable/sp_MSforeachdb
- 两个有用的,ms未公开的存储过程sp_MSforeachDB,sp_MSforeachtable
- 【推荐】(SqlServer)不公开存储过程sp_Msforeachtable与sp_Msforeachdb详解
- 【推荐】(SqlServer)不公开存储过程sp_Msforeachtable与sp_Msforeachdb详解
- sp_MSforeachtable 内置存储过程的运用
- 微软不公开存储过程sp_Msforeachtable与sp_Msforeachdb详解-统计数据库-记录数-容量-存储过程脚本等
- 详解SQL Server的两个存储过程:sp_MSforeachtable/sp_MSforeachdb
- 【推荐】(SqlServer)不公开存储过程sp_Msforeachtable与sp_Msforeachdb详解