我有SQL query
选择一些值(并以连接的形式跨表)。其中一个字段返回0或1,但我希望在0的情况下返回其他内容,或者返回1的情况(是/否)。
代码:
SELECT Passed,
CASE Passed
WHEN '1' THEN 'Yes'
ELSE 'No'
END
FROM EXAM_INSTANCE
完美地运作。
但是当我将它作为子查询集成到存储过程中时,我得到错误:
Only one expression can be specified in the select list when the sub-query is not introduced with EXISTS.
原始查询是:
select Firstname, Lastname, ei.Started,
((ei.totalcorrect*100)/@examQuestionCount) as percentage, passed, ei.InstanceID
from ea ei, INVITE i, OSTTable ost, f f
where ei.Finished is not null
and ei.InviteID = i.InviteID
我在尝试:
select Firstname, Lastname, ei.Started,
((ei.totalcorrect*100)/@examQuestionCount) as percentage,
(SELECT Passed,
CASE Passed
WHEN '1' THEN 'Yes'
ELSE 'No'
END
FROM EA),
ei.InstanceID
from EA E ei, INVITE i, osttable ost, ef e
where ei.Finished is not null
and ei.InviteID = i.InviteID
我的错误是什么原因造成的?
PS我出于隐私原因更改了表名,所以如果它们不匹配,请不要担心。
答案 0 :(得分:2)
该位置的子查询应返回不超过一个值(即一行中的一列),但子查询返回两列(一列为Passed
,另一列为未命名的列,由CASE表达式)我不知道有多少行。
您应该将子查询的结果集成到主查询中(类似于@Alex_L建议的内容)或将子查询的输出限制为预期的值数量。
答案 1 :(得分:1)
select Firstname, Lastname, ei.Started,
((ei.totalcorrect*100)/@examQuestionCount) as percentage,
passed_ = CASE Passed
WHEN '1' THEN 'Yes'
ELSE 'No'
END,
ei.InstanceID
from EA E ei, INVITE i, osttable ost, ef e
where ei.Finished is not null
and ei.InviteID = i.InviteID
答案 2 :(得分:0)
USE [IAS_AccountDB]
GO
/* Object: StoredProcedure [dbo].[Prc_Accounts_LastNodes] Script Date: 04/01/2013 01:29:20 */
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[Prc_Accounts_LastNodes]
(
@AccountParentID int = NULL
)
AS
SELECT
[Accounts].[AccountName]
,[Accounts].[AccountID]
FROM
[Accounts]
WHERE
@AccountParentID IN
(
SELECT
[Accounts].[AccountID]
,[Accounts].[AccountName]
,[Accounts].[AccountStandardCode]
FROM
[Accounts]
WHERE
NOT EXISTS
(
SELECT TOP 1
1
FROM
[Accounts_ParentIDs]
WHERE
[Accounts_ParentIDs].[AccountParentID] = [Accounts].[AccountID]
)
)
RETURN 0;