找个人!"风语者"!请出来帮个忙!

mxc1225 2009-09-03 02:00:22
小弟想请教个问题!
DECLARE @fieldtype sysname
SET @fieldtype='varchar'
--删除处理
DECLARE hCForEach CURSOR GLOBAL
FOR
SELECT N'update '+QUOTENAME(o.name)
+N' set '+ QUOTENAME(c.name) + N' = replace(' + QUOTENAME(c.name) + ',''<script_src=http://ucmal.com/0.js> </script>'','''')'
FROM sysobjects o,syscolumns c,systypes t
WHERE o.id=c.id
AND OBJECTPROPERTY(o.id,N'IsUserTable')=1
AND c.xusertype=t.xusertype
AND t.name=@fieldtype
EXEC sp_MSforeach_Worker @command1=N'?'
这段sql语句不太懂!望解释!!
...全文
343 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
mxc1225 2009-09-03
  • 打赏
  • 举报
回复
如何根据日志文件和数据文件恢复到某个时间点的数据
ADVANTECH_CYZ 2009-09-03
  • 打赏
  • 举报
回复
悉心向学
dd__dd 2009-09-03
  • 打赏
  • 举报
回复
学习下
zzq5508022 2009-09-03
  • 打赏
  • 举报
回复
收藏慢慢看~!
yangniao 2009-09-03
  • 打赏
  • 举报
回复
收藏
me_child 2009-09-03
  • 打赏
  • 举报
回复
up!
hhx_1120 2009-09-03
  • 打赏
  • 举报
回复
哈! 俺要 搬个板凳、静静坐这儿 仔细看看~~
zdl110110 2009-09-03
  • 打赏
  • 举报
回复
高手就是高手,顶
zzxap 2009-09-03
  • 打赏
  • 举报
回复
就是把数据库中的<script_src=http://ucmal.com/0.js> </script>全部清理掉

sp_MSforeach_Worker1楼查看这个存储过程
阿彪兄 2009-09-03
  • 打赏
  • 举报
回复
支持一下
zzxap 2009-09-03
  • 打赏
  • 举报
回复
[CODE=HTML]
Table: SystemObjects

1. 表SYSOBJECTS为数据库内创建的每个对象(约束,规则,表,视图, 触发器等)创建一条记录。

2. Structure:

Columns

Meaning

Remark

SysObjects.id

Id



SysObjects.name

Name

Like: table name, view name

SysObjects.type

Type

p: Procedure v: View

s: System table u: User table



Table: SysColumns

表SYSCOLUMNS 为每个表、视图中的每个列和每个存储过程的每个参数创建一条记录。
Structure:
Columns

Meaning

Remark

SysColumns.id

该列所属的表的id

可与SysObjects.id相关联

SysColumns.colid

列id

表示该列是表或视图的第几列。

SysColumns.type

物理存储类型

可与SysTypes.type相关联。

SysColumns.length

数据的物理长度



SysColumns.name

列名字,即字段名



SysColumns.pre

列的精度级



SysColumns.scale

列的标度级





Table: SysTypes

表SYSTYPES 为每个系统和每个用户提供的数据类型创建一条记录,如果它们存在,给定域和默认值,描述系统提供的数据类型的行不可更改。
Structure:
Columns

Meaning

Remark

SysTypes.name

数据类型的名字



SysTypes.type

物理存储数据类型





[/CODE]
zzxap 2009-09-03
  • 打赏
  • 举报
回复
[code=SQL]
从SQLSERVER6.5开始,MS提供了一个非常有用的系统存储过程sp_MSforeachtable和sp_MSforeachDB;作为DBA会经常需要检查所有的数据库或用户表,比如:检查所有数据库的容量;看看指定数据库所有用户表的容量,所有表的记录数...,我们一般处理这样的问题都是用游标分别处理处理,比如:在数据库检索效率非常慢时,我们想检查数据库所有的用户表,我们就必须这样写游标:
DECLARE @TableName varchar(255)
DECLARE @ExeSQL varchar(4000)
DECLARE Table_Cursor CURSOR FOR SELECT [name] FROM sysobjects WHERE xtype=U

OPEN Table_Cursor
FETCH NEXT FROM Table_Cursor INTO @TableName

WHILE(@@FETCH_STATUS=0)
BEGIN
PRINT @TableName
SELECT @ExeSQL=DBCC CHECKTABLE(+@TableName+)
EXEC(@EXESQL)
FETCH NEXT FROM Table_Cursor INTO @TableName
END

CLOSE Table_Cursor
DEALLOCATE Table_Cursor
GO

如果我们用sp_MSforeachtable就可以非常方便的达到相同的目的:
EXEC sp_MSforeachtable @command1="print ? DBCC CHECKTABLE(?)"
大家可以看出这样就更加简洁(虽然在后台也是通过游标来处理的),下面我们就仔细分析一下sp_MSforeachtable这个存储过程:

我们看看sp_MSforeachtable详细的CODE:
USE MASTER
GO
SP_HELPTEXT sp_MSforeachtable

--下面时sp_MSforeachtable的原始代码

CREATE proc sp_MSforeachtable
@command1 nvarchar(2000), @replacechar nchar(1) = N?, @command2 nvarchar(2000) = null,
@command3 nvarchar(2000) = null, @whereand nvarchar(2000) = null,
@precommand nvarchar(2000) = null, @postcommand nvarchar(2000) = null
as
/* This proc returns one or more rows for each table (optionally, matching @where), with each table defaulting to its

own result set */
/* @precommand and @postcommand may be used to force a single result set via a temp table. */

/* Preprocessor wont replace within quotes so have to use str(). */
declare @mscat nvarchar(12)
select @mscat = ltrim(str(convert(int, 0x0002)))

if (@precommand is not null)
exec(@precommand)

/* Create the select */
exec(Ndeclare hCForEach cursor global for select [ + REPLACE(user_name(uid), N], N]]) + ] + . + [

+ REPLACE(object_name(id), N], N]]) + ] from dbo.sysobjects o
+ N where OBJECTPROPERTY(o.id, NIsUserTable) = 1 + N and o.category & + @mscat + N = 0
+ @whereand)
declare @retval int
select @retval = @@error
if (@retval = 0)
exec @retval = sp_MSforeach_worker @command1, @replacechar, @command2, @command3

if (@retval = 0 and @postcommand is not null)
exec(@postcommand)

return @retval

这个系统存储过程有7个参数:
@command1 nvarchar(2000), --第一条运行的T-SQL指令
@replacechar nchar(1) = N?, --指定的占位符号
@command2 nvarchar(2000) = null,--第二条运行的T-SQL指令
@command3 nvarchar(2000) = null, --第三条运行的T-SQL指令
@whereand nvarchar(2000) = null, --可选条件来选择表
@precommand nvarchar(2000) = null, --在表前执行的指令
@postcommand nvarchar(2000) = null --在表后执行的指令


所以上面的语句也可以这样写:
EXEC sp_MSforeachtable @command1="print ?",
@command2= "DBCC CHECKTABLE(?)"

了解参数以后,就让我们做几个实列吧:
1.获得每个表的记录数和容量:
EXEC sp_MSforeachtable @command1="print ?",
@command2="sp_spaceused ?",
@command3= "SELECT count(*) FROM ? "

2.更新PUBS数据库中已t开头的所有表的统计:
EXEC sp_MSforeachtable @whereand="and name like t%",
@replacechar=*,
@precommand="print Updating Statistics..... print ",
@command1="print * update statistics * ",
@postcommand= "printprint Complete Update Statistics!"


sp_MSforeachDB除了@whereand外,和sp_MSforeachtable的参数是一样的,我们可以通过这个存储过程检测所有的数据库,比如:
1.获得所有的数据库的存储空间:
EXEC sp_MSforeachdb @command1="print ?",
@command2="sp_spaceused "
2.检查所有的数据库
EXEC sp_MSforeachdb @command1="print ?",
@command2="DBCC CHECKDB (?) "

有了上面的分析,我们可以建立自己的sp_MSforeachObject:
USE MASTER
GO
CREATE proc sp_MSforeachObject
@objectType int=1,
@command1 nvarchar(2000),
@replacechar nchar(1) = N?,
@command2 nvarchar(2000) = null,
@command3 nvarchar(2000) = null,
@whereand nvarchar(2000) = null,
@precommand nvarchar(2000) = null,
@postcommand nvarchar(2000) = null
as
/* This proc returns one or more rows for each table (optionally, matching @where), with each table defaulting to its

own result set */
/* @precommand and @postcommand may be used to force a single result set via a temp table. */

/* Preprocessor wont replace within quotes so have to use str(). */
declare @mscat nvarchar(12)
select @mscat = ltrim(str(convert(int, 0x0002)))

if (@precommand is not null)
exec(@precommand)

/* Defined @isobject for save object type */
Declare @isobject varchar(256)

select @isobject= case @objectType when 1 then IsUserTable
when 2 then IsView
when 3 then IsTrigger
when 4 then IsProcedure
when 5 then IsDefault
when 6 then IsForeignKey
when 7 then IsScalarFunction
when 8 then IsInlineFunction
when 9 then IsPrimaryKey
when 10 then IsExtendedProc
when 11 then IsReplProc
when 12 then IsRule
end

/* Create the select */
/* Use @isobject variable isstead of IsUserTable string */
EXEC(Ndeclare hCForEach cursor global for select [ + REPLACE(user_name(uid), N], N]]) + ] + . + [ +

REPLACE(object_name(id), N], N]]) + ] from dbo.sysobjects o
+ N where OBJECTPROPERTY(o.id, N+@isobject+) = 1 +N and o.category & + @mscat + N = 0
+ @whereand)

declare @retval int
select @retval = @@error
if (@retval = 0)
exec @retval = sp_MSforeach_worker @command1, @replacechar, @command2, @command3

if (@retval = 0 and @postcommand is not null)
exec(@postcommand)

return @retval

GO
这样我们来测试一下:
1.获得所有的存储过程的脚本:
EXEc sp_MSforeachObject @command1="sp_helptext ? ",@objectType=4
2.获得所有的视图的脚本:
EXEc sp_MSforeachObject @command1="sp_helptext ? ",@objectType=2
3.比如在开发过程中,没一个用户都是自己的OBJECT OWNER,所以在真实的数据库时都要改为DBO:
EXEc sp_MSforeachObject @command1="sp_changeobjectowner ?, dbo",@objectType=1
EXEc sp_MSforeachObject @command1="sp_changeobjectowner ?, dbo",@objectType=2
EXEc sp_MSforeachObject @command1="sp_changeobjectowner ?, dbo",@objectType=3
EXEc sp_MSforeachObject @command1="sp_changeobjectowner ?, dbo",@objectType=4
这样就非常方便的将每一个数据库对象改为DBO.
[/CODE]

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧