SQL Server常用存储过程及示例常用存储过程及示例
分页:分页:
代码如下:
/*分页查找数据*/
CREATE PROCEDURE [dbo].[GetRecordSet]
@strSql varchar(8000),–查询sql,如select * from [user]
@PageIndex int,–查询当页号
@PageSize int–每页显示记录
AS
set 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
用户注册:用户注册:
代码如下:
/*
用户注册,也算是添加吧
*/
Create proc [dbo].[UserAdd]
(
@loginID nvarchar(50), –登录帐号
@password 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,address,email,createTime,status)
values
(@userID,@loginID,@password,”,”,”,@email,getdate(),1)
return @userID
end
SQL Server 系统存储过程
1. 给表中字段添加描述信息
代码如下:
Create table T2 (id int , name char (20))
GO
EXEC sp_addextendedproperty ‘MS_Description’, ‘Employee ID’, ‘user’, dbo, ‘table’, T2, ‘column’, id