【发布时间】:2013-04-17 07:17:32
【问题描述】:
我有一个 select * from book table 的存储过程,使用子查询我的查询是
USE [library]
GO
/****** Object: StoredProcedure [dbo].[report_r_and_l] Script Date: 04/17/2013 12:42:39 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[report_r_and_l]
@fdate date,
@tdate date,
@key varchar(1)
as
if(@key='r')
select *
from dbo.books
where isbn =(select isbn from dbo.lending where (act between @fdate and @tdate) and (stat ='close'))
else if(@key='l')
select *
from dbo.books
where isbn =(select isbn from dbo.lending where lended_date between @fdate and @tdate)
我知道子查询向主查询返回多个查询,但我不知道如何避免此错误,谁能帮助我?
【问题讨论】:
-
显然
select isbn返回的值不止一个。你可以使用where isbn IN (select isbn ... -
使用
select top 1 isbn ...或select max(isbn) ...。即使你知道只会返回一行,sql-server 也不知道,因为错误信息是基于对命令的静态分析,而不是基于真实数据。
标签: sql-server subquery