sqlserver存储过程集锦(二)

原创|其它|编辑:郝浩|2009-09-30 11:11:03.000|阅读 581 次

概述:本文总结了mssql常用的一些存储过程集锦。

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

  ====================数据库还原的存储过程============

  SQL code
  create proc killspid (@dbname varchar(20))
  as
  begin
  declare @sql nvarchar(500)
  declare @spid int
  set @sql='declare getspid cursor for
  select spid
  from sysprocesses
  where dbid=db_id('''+@dbname+''')'
  exec (@sql)
  open getspid
  fetch next from getspid
  into @spid
  while @@fetch_status <>-1
  begin
  exec('kill '+@spid)
  fetch next from getspid
  into @spid
  end
  close getspid
  deallocate getspid
  end
  GO

  作用:杀掉传入数据库中的活动进程以进行备份还原等独占操作

  ===================阿拉伯数字转大写中文=============

  例:输入12345,程序给出:壹万贰仟叁佰肆拾伍

  例:输入10023040,程序给出:壹仟另贰万叁仟另肆拾

  解决方案之一(在SqlServer2000中测试通过):

  SQL code
  CREATE FUNCTION fun_cgnum
  (@num INT)
  RETURNS VARCHAR(100)
  AS
  BEGIN
  DECLARE @temp INT,@res INT,@i TINYINT
  DECLARE @str VARCHAR(100),@no VARCHAR(20),@unit VARCHAR(16)
  SELECT @str='',@no='另壹贰叁肆伍陆柒捌玖',@unit='拾佰仟万拾佰仟亿'
  SET @temp=@num

  SELECT @i=0,@res=@temp%10,@temp=@temp/10
  WHILE @temp>0
  BEGIN
  IF @i=0
  SET @str=SUBSTRING(@no,@res+1,1)
  ELSE
  SET @str=SUBSTRING(@no,@res+1,1)+SUBSTRING(@unit,@i,1)+@str
  SELECT @res=@temp%10,@temp=@temp/10
  SET @i=@i+1
  END
  SET @str=SUBSTRING(@no,@res+1,1)+SUBSTRING(@unit,@i,1)+@str
  SET @str=REPLACE(@str,'另拾','另')
  SET @str=REPLACE(@str,'另佰','另')
  SET @str=REPLACE(@str,'另仟','另')
  SET @str=REPLACE(@str,'另拾','另')
  SET @str=REPLACE(@str,'另万','万')
  WHILE @i>0
  BEGIN
  SET @str=REPLACE(@str,'另另','另')
  SET @i=CHARINDEX('另另',@str)
  END
  SET @str=REPLACE(@str,'另万','万')
  SET @str=REPLACE(@str,'亿万','亿')
  IF RIGHT(@str,1)='另'
  SET @str=LEFT(@str,LEN(@str)-1)
  RETURN @str
  END
  GO

  --测试:有0和没有0的情况

  SELECT dbo.fun_cgnum(900000000),dbo.fun_cgnum(903002051),dbo.fun_cgnum(903002050)

  PS:有兴趣的朋友可以继续考虑有小数点以及添加单位(元/角/分)的情况


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com

文章转载自:IT专家网

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP