启天JAVA培训社区's Archiver

junsan 发表于 2009-9-30 16:57

sqlserver 常用存储过程集锦

常用存储过程集锦,都是一些mssql常用的一些,大家可以根据需要选择使用。  =================分页==========================
[table=95%][tr][td][b][/b]
  /*分页查找数据*/
  CREATE PROCEDURE [dbo].[GetRecordSet]
  @strSql varchar(8000),--查询sql,如select * from [user]
  @PageIndex [b][color=#bc0e07]int[/color][/b],--查询当页号
  @PageSize int--每页显示记录
  AS
  [b][color=#bc0e07]set[/color][/b] nocount on
  declare @p1 int
  declare @currentPage int
  set @currentPage = 0
  declare @RowCount int
  set @RowCount = 0
  declare @PageCount int
  set @PageCount = 0
  exec sp_cursoropen @p1 output,@strSql,@scrollopt=1,@ccopt=1,@rowcount=@rowCount output --得到总记录数
  select @PageCount=ceiling(1.0*@rowCount/@pagesize) --得到总页数
  ,@currentPage=(@PageIndex-1)*@PageSize+1
  select @RowCount,@PageCount
  exec sp_cursorfetch @p1,16,@currentPage,@PageSize
  exec sp_cursorclose @p1
  set nocount off
  GO[/td][/tr][/table]
[table=95%][tr][td][b][/b]
  =========================用户注册============================
  /*
  用户注册,也算是添加吧
  */
  Create proc [dbo].[UserAdd]
  (
  @loginID nvarchar(50),     --登录帐号
  @[b][color=#bc0e07]password[/color][/b] nvarchar(50), --密码
  @email nvarchar(200) --电子信箱
  )
  as
  declare @userID int --用户编号
  --登录账号已经被注册
  if exists(select loginID from tableName where loginID = @loginID)
  begin
  return -1;
  end
  --邮箱已经被注册
  else if exists(select email from tableName where email = @email)
  begin
  return -2;
  end
  --注册成功
  else
  begin
  select @userID = isnull(max(userID),100000)+1 from tableName
  insert into tableName
  (userID,loginID,[password],userName,linkNum,[b][color=#bc0e07]address[/color][/b],email,createTime,status)
  values
  (@userID,@loginID,@password,'','','',@email,getdate(),1)
  return @userID
  end[/td][/tr][/table]

junsan 发表于 2009-9-30 16:57

==========================sql [b][color=#bc0e07]server[/color][/b]系统存储过程===================[table=95%][tr][td][b][/b]
  

junsan 发表于 2009-9-30 16:58

====================数据库还原的存储过程============[table=95%][tr][td][b][/b]
  SQL [b][color=#bc0e07]code[/color][/b]
  create proc killspid (@dbname varchar(20))
  as
  begin
  declare @sql nvarchar(500)
  declare @spid [b][color=#bc0e07]int[/color][/b]
  [b][color=#bc0e07]set[/color][/b] @sql='declare getspid [b][color=#bc0e07]cursor[/color][/b] for
  select spid
  from sysprocesses
  where dbid=db_id('''+@dbname+''')'
  exec (@sql)
  open getspid
  [b][color=#bc0e07]fetch[/color][/b] 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[/td][/tr][/table]
  作用:杀掉传入数据库中的活动[b][color=#bc0e07]进程[/color][/b]以进行备份还原等独占操作
  ===================阿拉伯数字转大写中文=============
  例:输入12345,程序给出:壹万贰仟叁佰肆拾伍
  例:输入10023040,程序给出:壹仟另贰万叁仟另肆拾
  解决方案之一(在SqlServer2000中测试通过):
[table=95%][tr][td][b][/b]
  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[/td][/tr][/table]

junsan 发表于 2009-9-30 16:58

[table=95%][tr][td][b][/b]
  SELECT @i=0,@res=@temp%10,@temp=@temp/10
  WHILE @temp>0
  BEGIN
  IF @i=0
  [b][color=#bc0e07]SET[/color][/b] @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[/td][/tr][/table]  --测试:有0和没有0的情况
  SELECT dbo.fun_cgnum(900000000),dbo.fun_cgnum(903002051),dbo.fun_cgnum(903002050)
  PS:有兴趣的朋友可以继续考虑有小数点以及添加单位(元/角/分)的情况

页: [1]

Powered by Discuz! Archiver 6.1.0F  © 2001-2007 Comsenz Inc.