drop proc 存储过程名
我们还可使用management studio来管理存储过程,展开菜单树中的“可编程性”,在“存储过程”的子节点中可以进行各种操作。这里要说一下创建:当点击“新建存储过程”之后,会出现一个基于模板的创建语句。这时点击菜单中的“查询→指定模板参数的值”,即可弹出对话框来对模板进行设置,从而建立我们想要的存储过程。另外,点击菜单中的“视图→模板资源管理器”,可以看到SQL SERVER为我们提供的各种SQL语句模板。 【一个简单的例子】
--插入一个以时间为用户名的用户
create proc insUser
as
begin tran
declare @username varchar(20)
set @username=convert(varchar(8),getdate(),112)
+replace(convert(varchar(10),getdate(),8),':','')
if not exists(select * from yonghu where yonghuming=@username)
insert into yonghu values
(@username,'111111','@163.com','新用户')
commit tran --也可以写commit,但是建议不要去掉tran
go
然后使用exec执行这个存储过程:
exec insUser
create|alter proc 存储过程名
@参数名参数类型[,
@参数名 参数类型...]
as
批处理语句
go
还记得我们前面做过的一个案例吗?
declare @tablename nvarchar(10),@id varchar(10),@idvalue int
declare @sql varchar(100)
set @tablename='yiren'
set @id='yirenid'
set @idvalue=10
set @sql='select * from '+@tablename+' where '+@id+' = '+cast(@idvalue as varchar)
print @sql
exec(@sql)
现在我们把它写成存储过程。这样我们每次都可以从一个指定表中提取我们想要的记录了
create proc queryItem
@tablename nvarchar(10),
@id varchar(20),
@idvalue int --参数外面还可以套上圆括号,看起来更加清晰
as
declare @sql varchar(100)
set @sql='select * from '+@tablename+'
where '+@id+' = '+cast(@idvalue as varchar)
exec(@sql)
go
create proc returnProc
as
begin tran
declare @error int
insert into yiren (xingming) values ('王美丽')
set @error=@@error
insert into yiren (yirenid) values (1)
set @error=@error+@@error
if @error>0
rollback tran
else
commit tran
return @error
go
create proc queryProfile
@id int,
@xingming varchar(50) output, --必须有output
@nianling int output
as
select @xingming=xingming,@nianling=nianling
from yiren where yirenid=@id
go
调用的方法:
declare @xingming varchar(50),@nianling int
exec queryProfile 1,@xingming output,@nianling output --必须有output
print '1号艺人的姓名是'+@xingming
+',年龄是'+cast(@nianling as varchar)+'岁'
create proc proc_1 --获取艺人id
@yid int output
as
select top 1 @yid=yirenid from yiren
where nicheng='芙蓉姐姐'
print '存储过程1在第'+cast(@@NESTLEVEL as varchar)+'层'
go
create proc proc_1_1 --获取粉丝id
@fid int output
as
declare @yid int
exec proc_1 @yid output
select @fid=yonghuid from fensi
where yirenid=@yid
order by yonghuid desc
print '存储过程1_1在第'+cast(@@NESTLEVEL as varchar)+'层'
go
create proc proc_1_1_1 --获得芙蓉姐姐的粉丝
as
declare @fid int
exec proc_1_1 @fid output
select * from yonghu
where yonghuid=@fid
print '存储过程1_1_1在第'+cast(@@NESTLEVEL as varchar)+'层'
go
create proc queryPage
@tablename nvarchar(50), --用于传入表名
@idname nvarchar(50), --用于传入字段名
@pagesize int, --用于传入每页记录数
@currentpage int, --用于传入希望查看的页面编号
@totalpages int output --用于传出页面总数
as
--声明保存查询语句的局部变量:
declare @sql as nvarchar(1000)
--声明保存记录总数的局部变量:
declare @rowcount as int
--获得记录总数:
set @sql='select @rc=count(*) from '+@tablename
--不要直接执行select @rowcount=count(*) from @tablename
--将参数传入语句:
exec sp_executesql @sql,N'@rc int output',@rc=@rowcount output
--将根据每页的行数得到的总页数保存到输出参数中:
set @totalpages = ceiling(cast(@rowcount as float)/cast(@pagesize as float))
if @currentpage >1
begin
if @currentpage>@totalpages
begin
set @currentpage = @totalpages --则显示最后一页
end
set @sql = 'select top '+cast(@pagesize as varchar)
+' * from '+@tablename+' where '+@idname+' not in (select top '
+cast(@pagesize*(@currentpage-1) as varchar)
+' '+@idname+' from '+@tablename+' order by '+@idname+')
order by '+@idname
end
else --只选第一页就不必使用子查询了,提高性能
begin
set @sql = 'select top '+cast(@pagesize as varchar)
+' * from '+@tablename+' order by '+@idname
end
exec(@sql) --执行查询语句
go