sql2000存储过程传递函数的问题

farway 2002-05-11 10:49:53
有这么一段程序:
use pubs
go

if exists(select name from sysobjects
where name = 'titles_sum' and type ='p')
drop procedure titles_sum
go

use pubs
go

create procedure titles_sum @title varchar(40)='%',@sum money output
as
select 'title name'=title
from titles
where title like @title
select @sum=sum(price)
from titles
where title like @title
go


declare @titlecost money
execute titles_sum 'the%',@totalcost output
if @totalcost<200
begin
print ' '
print 'all of these titles can be purchased for less than $200.'
end
else
select 'the total cost of these titles is $'+rtrim (@totalcost as
varchar(20)))

大虾能帮我详细解释一下这个程序吗
...全文
62 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
gongshutao 2002-06-05
  • 打赏
  • 举报
回复
最近没有看历史贴
use pubs --使用pubs数据库
go --执行
--sysobjects表示一个系统表,里面存储数据库的对象type ='p'代表存储过程
if exists(select name from sysobjects
where name = 'titles_sum' and type ='p') --检查名称为titles_sum的存储过程是否存在
drop procedure titles_sum --删除存储过程titles_sum
go

use pubs
go
--创建存储过程titles_sum
--sql里面变量必须用@开头
--声明参数@title类型 varchar 长度40 输入参数(默认)
--参数@sum类型 money 输出参数(output)
create procedure titles_sum @title varchar(40)='%',@sum money output
as
select 'title name'=title from titles where title like @title
--返回一个结果集
--从表titls选择title列,把这个列的标题显示为 title name ,条件 like 传进来的第一个参数
select @sum=sum(price) from titles where title like @title
--统计 满足条件的price这个字段的和 并且把统计值赋给参数@sum(此参数是一个输出参数)
go

declare @titlecost money
--定义一个money类型的变量
execute titles_sum 'the%',@totalcost output
--执行存储过程titles_sum (就上边定义的)
--执行结果(在查询分析器里)
--网格这一页 显示三条纪录 以 'the' 开头的纪录
--消息这一页 输出 all of these titles can be purchased for less than $200.
--这里有点问题 @totalcost 没有声明,应该是把
--declare @titlecost money 改为
--declare @totalcost money
--
if @totalcost<200
begin
print ' '
print 'all of these titles can be purchased for less than $200.'
end
else
--select 'the total cost of these titles is $'+rtrim (@totalcost as varchar(20)))
--这行有错误,应该改为
select 'the total cost of these titles is $'+rtrim(cast(@totalcost as varchar(20)))
--根据执行结果(统计值(@totalcost))显示不同的信息。
--你可以试试下面的
declare @totalcost money
execute titles_sum '%',@totalcost output
if @totalcost<200
begin
print ' '
print 'all of these titles can be purchased for less than $200.'
end
else
select 'the total cost of these titles is $'+rtrim(cast(@totalcost as varchar(20)))
--会返回两个结果集
--第二个结果集是select 'the total cost of these titles is $'+rtrim(cast(@totalcost as varchar(20)))返回的结果
farway 2002-05-12
  • 打赏
  • 举报
回复
哪位大虾再帮帮我啊
farway 2002-05-11
  • 打赏
  • 举报
回复
老兄啊!到了关键时刻,我可等着你那!
多谢多谢!
gongshutao 2002-05-11
  • 打赏
  • 举报
回复
use pubs --使用pubs数据库
go --执行
--sysobjects表示一个系统表,里面存储数据库的对象type ='p'代表存储过程
if exists(select name from sysobjects
where name = 'titles_sum' and type ='p') --检查名称为titles_sum的存储过程是否存在
drop procedure titles_sum --删除存储过程titles_sum
go

use pubs
go
--创建存储过程titles_sum
--sql里面变量必须用@开头
--声明参数@title类型 varchar 长度40 输入参数(默认)
--参数@sum类型 money 输出参数(output)
create procedure titles_sum @title varchar(40)='%',@sum money output

吃饭去先:)

684

社区成员

发帖
与我相关
我的任务
社区描述
智能路由器通常具有独立的操作系统,包括OpenWRT、eCos、VxWorks等,可以由用户自行安装各种应用,实现网络和设备的智能化管理。
linuxpython 技术论坛(原bbs)
社区管理员
  • 智能路由器社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧