今天看书学了点数据库(sql server)的基础知识,特此记录一下:
1.存储过程由return命令返回一个状态码,返回码0表示成功;从-1至-14的值表示不同类型的错误;而-15至-99保留给以后使用.
2.下面是一个在存储过程中使用游标参数返回一个结果集
use pubs
if object_id('dbo.listsales') is not null
drop proc dbo.listsales
create proc dbo.listsales @bestseller tid out,@topsales int out,@salescursor cursor varying out
as
select @bestseller=bestseller, @topsales=totalsales from ( select top 1 title_id as bestseller,sum(qty) as totalsales from sales group by title_id order by 2 desc) bestselle
declare s cursor local for select * from sales
open s
set @salescursor=s
return(0)
go
declare @topsales int, @bestseller tid,@salecursor cursor
exec dbo.listsales @bestseller out,@topsales out,@salescursor out
select @bestseller,@topsales fetch @salescursor
close @salescursor
deallocate @salescurso
--其中varying关键字是游标参数所需要的,表示返回值是不定的,也就是说,它可以返回多个值,游标参数吸能是输出参数