sql中的substring和charindex

 不管是程序还是存储过程,要养成良好的注释习惯!
不啰嗦。update a set FileName= substring(FileName,charindex('/',FileName)+1,len(FileName))
哈~最近喜欢上数据库和正则!

RAISERROR

用户定义的与消息关联的严重级别。用户可以使用从 0 到 18 之间的严重级别。19 到 25 之间的严重级别只能由 sysadmin 固定服务器角色成员使用。若要使用 19 到 25 之间的严重级别,必须选择 WITH LOG 选项。

DBCC SQLPERF  perf(活跃的)

提供有关所有数据库的事务日志空间使用情况的统计信息

语法:  DBCC SQLPERF LOGSPACE

(在 存储过程中 用exec ('DBCC SQLPERF (LogSpace)')

所包含的字段有DatabaseName Log SizeMB)、Log Space Used(%)、Status
假如用存储过程检测数据库分配的空间是否超出分配的空间的某个值,并对数据库进行备份:

CREATE Procedure prBackupIfLogAlmostFull
-- Do backup of transaction log if percent of space used is bigger then @fltPercentLimit
 (
  @chvDbName sysname,
  @fltPercentLimit float,
  @debug int = 0
 )

As
set nocount on

declare @intErrorCode int,
  @fltPercentUsed float,
  @chvDeviceName sysname,
  @chvFileName sysname

set @intErrorCode = @@Error

-- how much of log space is used at the moment
if @intErrorCode = 0
 exec @intErrorCode = prLogSpacePercentUsed @chvDbName, @fltPercentUsed OUTPUT

-- if limit is not reached, just go out
if @intErrorCode = 0 and @fltPercentUsed < @fltPercentLimit
 return 0

if @intErrorCode = 0
begin
 Set @chvDeviceName = @chvDbName + Convert(Varchar, GetDate(), 112)
 Set @chvFileName = 'c:\mssql7\backup\bkp'+ @chvDeviceName + '.dat'
 set @intErrorCode = @@Error  
end

if @debug <> 0
 select @chvDeviceName chvDeviceName, @chvFileName chvFileName

if @intErrorCode = 0
begin
 EXEC sp_addumpdevice 'disk', @chvDeviceName, @chvFileName
 set @intErrorCode = @@Error  
end

-- 15061 it is OK if dump device already exists
if @intErrorCode = 0 or @intErrorCode = 15061
begin
 BACKUP LOG @chvDbName TO @chvDeviceName
 set @intErrorCode = @@Error  
end

return @intErrorCode


GO



CREATE Procedure prLogSpacePercentUsed
-- return percent of space used in transcation log for specified database
 (
  @chvDbName sysname,
  @fltPercentUsed float OUTPUT
 )

As
set nocount on

declare @intErrorCode int

set @intErrorCode = @@Error

if @intErrorCode = 0
begin
 create table #DBLogSpace
  ( dbname sysname,
   LogSizeInMB float,
   LogPercentUsed float,
   Status int
  )
 set @intErrorCode = @@Error  
end

-- get log space info. for all databases
if @intErrorCode = 0
begin
 insert into #DBLogSpace
  exec ('DBCC SQLPERF (LogSpace)') -- it will not work without exec
 set @intErrorCode = @@Error  
end

-- get percent for specified database
if @intErrorCode = 0
begin
 select @fltPercentUsed = LogPercentUsed
 from #DBLogSpace
 where dbname = @chvDbName

 set @intErrorCode = @@Error  
end

drop table #DBLogSpace

return @intErrorCode

GO


测试sql语句的运行时间
在各个select语句前加:declare @d datetime
set @d=getdate()
并在select语句后加:
select [语句执行花费时间(毫秒)]=datediff(ms,@d,getdate())


 

你可能感兴趣的:(substring)