+ -
当前位置:首页 → 问答吧 → 存储过程中 带输出参数的问题。

存储过程中 带输出参数的问题。

时间:2011-11-16

来源:互联网

SQL code

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go


ALTER proc [dbo].[proc_new]
@id int=0,
@num int=100,
@returnid    int output
as
select top (@num) * from employee where id>@id
select @returnid = max([ID]) from (select top (@num) id from employee where id>@id ) as tmpTable


declare @id int

exec proc_new 2, 3, @id output
print @id  --这个打印的是数据库 employee表中最大的ID  而不是我想要的top (@num)中最大的ID。。
=================================================================================================
select @returnid = max([ID]) from (select top (@num) id from employee where id>@id ) as tmpTable这条有问题吗?
为什么我执行的时候他就给我打印数据库里边最大的ID  而不是在top(@num)里面找到最大的呢?

作者: a346729576   发布时间: 2011-11-16

select top (@num) * from employee where id>@id

在后面加上个order by 试试.例如:

select top (@num) * from employee where id>@id order by id

作者: dawugui   发布时间: 2011-11-16

SQL code

select max(id) from (select top 10 ID from employee where id>100) as t
--这样的话也是输出表中最大的ID  而不是我查询10条中最大的ID

作者: a346729576   发布时间: 2011-11-16

引用 1 楼 dawugui 的回复:

select top (@num) * from employee where id>@id

在后面加上个order by 试试.例如:

select top (@num) * from employee where id>@id order by id


成功.。谢谢 可是为什么呢....

作者: a346729576   发布时间: 2011-11-16

引用 2 楼 a346729576 的回复:

SQL code

select max(id) from (select top 10 ID from employee where id>100) as t
--这样的话也是输出表中最大的ID 而不是我查询10条中最大的ID

SQL code

CREATE proc [dbo].[proc_new]
@id int=0,
@num int=100,
@returnid    int output
as
select top (@num) * from employee where id>@id

select top (@num) @returnid  = id from employee where id>@id

go
--这是别人给的另一种方法..

select top (@num) @returnid  = id from employee where id>@id这句什么意思啊 。。看不懂..
加了top不是取得很多条数据吗?为什么结果就返回了一条呢

作者: a346729576   发布时间: 2011-11-16

select top (@num) @returnid = id from employee where id>@id这句什么意思啊 。。看不懂..
加了top不是取得很多条数据吗?为什么结果就返回了一条呢

赋值语句只取一个值,多个值无效.而且这种写法不推荐.

作者: dawugui   发布时间: 2011-11-16