消息512,级别16,状态1,过程测试,行21子查询返回的值超过1

时间:2011-01-10 17:44:00

标签: asp.net vb.net sql-server-2005 stored-procedures

ALTER PROCEDURE [dbo].[test]
@tour int,
@tourname varchar(50) OUTPUT,  
@tourdepartures varchar(50) OUTPUT
AS
BEGIN
 -- SET NOCOUNT ON added to prevent extra result sets from
 -- interfering with SELECT statements.
 SET NOCOUNT ON;

    -- Insert statements for procedure here
SET @tourname = (select [tour name] from dbo.products1 where tour = @tour)

SET @tourdepartures = (select ddate7 from dbo.TDEPART1 where tour = @tour and depart > convert(int,getdate()))

END

我想使用存储过程来填充我的asp.net页面上的标签 和下拉列表

@tourname将填充到单个标签

虽然@tourdepartures将是多个日期,我想要在下拉列表中

然而,当我运行我的sp我得到此错误

Msg 512, Level 16, State 1, Procedure test, Line 21
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

(1 row(s) affected)

(1 row(s) affected)

当我做的时候它会起作用

ALTER PROCEDURE [dbo].[test]
@tour int,
@tourname varchar(50) OUTPUT,  
@tourdepartures varchar(50) OUTPUT
AS
BEGIN
 -- SET NOCOUNT ON added to prevent extra result sets from
 -- interfering with SELECT statements.
 SET NOCOUNT ON;

    -- Insert statements for procedure here
SET @tourname = (select [tour name] from dbo.product where tour = @tour)

SET @tourdepartures = (select top 1 ddate7 from abcfilestest.dbo.TDEPART where tour = @tour and depart > convert(int,getdate()))

END

但它只给我第一个出发日期

3 个答案:

答案 0 :(得分:1)

删除第二个SET并返回一个可以使用阅读器的结果集:

ALTER PROCEDURE [dbo].[test]
@tour int,
@tourname varchar(50) OUTPUT
AS
BEGIN
 -- SET NOCOUNT ON added to prevent extra result sets from
 -- interfering with SELECT statements.
 SET NOCOUNT ON;

    -- Insert statements for procedure here
SET @tourname = (select [tour name] from dbo.products where tour = @tour)

select ddate7 from abcfilestest.dbo.TDEPART where tour = @tour and depart > convert(int,getdate())

END

答案 1 :(得分:0)

你可以尝试这个去除错误,但我认为这不是你想要的:

    -- Insert statements for procedure here
SET @tourname = (select TOP 1 [tour name] from dbo.products where tour = @tour)

SET @tourdepartures = (select TOP 1 ddate7 from abcfilestest.dbo.TDEPART where tour = @tour and depart > convert(int,getdate()))

如果您真的想要所有巡演日期的列表,请将最后一个声明更改为

select ddate7 from abcfilestest.dbo.TDEPART where tour = @tour and depart > convert(int,getdate())

并像往常一样解析客户端上的结果集。

此外,为了对所有圣洁的爱,将ddate7列重命名为有意义的内容。

答案 2 :(得分:0)

标量变量只能包含一个值。您正在使用的查询返回多个值。你有几个选择,正确的选择取决于你的情况。

如果您只想要最新日期,请在您的选择中使用前1名和日期排序。

如果您想要所有日期,则使用表变量而不是标量变量。我知道我可以在SQL Server 2008中使用表变量作为存储过程参数,不确定2005年。您可以只返回2个记录集(以便在返回的内容中保持一致)或一个输出变量和一个选择而不是使用输出变量