关于存储过程中的output的问题

我创建了两个存储过程 ,要显示指定pub_id 的一些信息
use pubs
go
create proc prcgreater
@pub_id char(4)
as
begin
declare @price money
declare @pubdate datetime
select @price=price,@pubdate=pubdate
from titles output
where pub_id=@pub_id
end

create proc prcgq
@pub_id char(4)
as
begin
declare @price money
declare @pubdate datetime
exec prcgreater @pub_id
if @@error<>0
begin
print 'error'
end
else
begin
select @price,@pubdate
end
end

exec prcgq 'BU1111' --这个号是存在的,为什么不显示@price,@pubdate

而显示:

1
服务器: 消息 266,级别 16,状态 2,过程 prcgreater,行 65535
EXECUTE 后的事务计数指出缺少了 COMMIT 或 ROLLBACK TRANSACTION 语句。原计数 = 0,当前计数 = 1。
error
服务器: 消息 266,级别 16,状态 2,过程 prcgq,行 65535
EXECUTE 后的事务计数指出缺少了 COMMIT 或 ROLLBACK TRANSACTION 语句。原计数 = 0,当前计数 = 1。

存储过程中也能用到事物吗

顺便问一句:怎样用OUTPUT将一个存储过程的变量值传给调用他的存储过程?

---------------------------------------------------------------

use pubs
go
create proc prcgreater
@pub_id char(4)
@price money output,
@pubdate datetime output
as
begin
select @price=price,@pubdate=pubdate
from titles
where pub_id=@pub_id
end

create proc prcgq
@pub_id char(4)
as
begin
declare @price money
declare @pubdate datetime
exec prcgreater @pub_id ,@price output,@pubdate output
if @@error<>0
begin
print 'error'
end
else
begin
select @price as price,@pubdate as pubdate
end
end

exec prcgq 'BU1111' --这个号是存在的,为什么不显示@price,@pubdate

---------------------------------------------------------------

exec prcgq 'BU1111' <------------ @pub_id char(4)

它的长度不对吧??!

---------------------------------------------------------------

output的应该是参数,要放在参数声明那里,而不是过程体内,

1
服务器: 消息 266,级别 16,状态 2,过程 prcgreater,行 65535
EXECUTE 后的事务计数指出缺少了 COMMIT 或 ROLLBACK TRANSACTION 语句。原计数 = 0,当前计数 = 1。
error
服务器: 消息 266,级别 16,状态 2,过程 prcgq,行 65535
EXECUTE 后的事务计数指出缺少了 COMMIT 或 ROLLBACK TRANSACTION 语句。原计数 = 0,当前计数 = 1。
是因为你的
select @price=price,@pubdate=pubdate from titles output where pub_id=@pub_id
的错误,
改成扬兄那样就对了!

---------------------------------------------------------------

不能在过程体内使用output来表示输出的,应该在存储过程的参数列表中声明。可以采用Yang_(扬帆破浪)的方法,也可以定义一个临时表,如下:
use pubs
go
create proc prcgreater
@pub_id char(4)
as
begin
DECLARE @price money,
@pubdate datetime

creat table #temptable
(price money,
pubdate datetime)

select @price=price,@pubdate=pubdate
from titles
where pub_id=@pub_id
insert into #temptable(price,pubdate)
values(@price,@pubdate)
end

create proc prcgq
@pub_id char(4)
as
begin
declare @price money
declare @pubdate datetime

creat table #temptable
(price money,
pubdate datetime)

insert exec prcgreater @pub_id

if @@error<>0
begin
print 'error'
end
else
begin
select * from #temptable
end
end

Published At
Categories with 数据库类
Tagged with
comments powered by Disqus