具体来说,我正在尝试创建变量,然后填充它们。
到目前为止我的源代码......
USE [AdventureWorks2012]
DECLARE @HighBonusAMT money, @LowBonusAMTnotzero money;
DECLARE @NumberofSalespeopleBonusZero INT, @NumberofSalespeopleGreaterthan4 INT, @NumberofSalespeopleGreaterthanzerolessthanorequalto4 INT;
SET @HighBonusAMT = (SELECT MAX(Bonus) FROM Sales.SalesPerson);
SET @LowBonusAMTnotzero = (SELECT MIN(Bonus) FROM Sales.SalesPerson WHERE Bonus <> $0);
SET @NumberofSalespeopleBonusZero = (SELECT BusinessEntityID FROM Sales.SalesPerson WHERE Bonus = $0);
SET @NumberofSalespeopleGreaterthan4 = (SELECT BusinessEntityID FROM Sales.SalesPerson WHERE Bonus > 4000);
SET @NumberofSalespeopleGreaterthanzerolessthanorequalto4 = (SELECT BusinessEntityID FROM Sales.SalesPerson WHERE Bonus > 0 AND Bonus <= 4000);
我的源代码自述文件......
创建变量,然后填充它们以存储:
当我收到错误消息时,我做错了什么:
Msg 512, Level 16, State 1, Line 20
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Msg 512, Level 16, State 1, 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.
Msg 512, Level 16, State 1, Line 22
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
答案 0 :(得分:1)
在以下情况下,您尝试为整数变量分配多个值:
SET @NumberofSalespeopleBonusZero = (SELECT BusinessEntityID, Bonus FROM Sales.SalesPerson WHERE Bonus = $0);
SET @NumberofSalespeopleGreaterthan4 = (SELECT BusinessEntityID, Bonus FROM Sales.SalesPerson WHERE Bonus > 4000);
SET @NumberofSalespeopleGreaterthanzerolessthanorequalto4 = (SELECT BusinessEntityID, Bonus FROM Sales.SalesPerson WHERE Bonus > 0 AND Bonus <= 4000);
您只能为这些变量分配一个值(原子)。
变量@NumberofSalespeopleBonusZero
,NumberofSalespeopleGreaterthan4
,@NumberofSalespeopleGreaterthanzerolessthanorequalto4
是整数类型,这意味着它们只能被赋予一个整数值,即10或251或1001等值。
以下查询输出两个结果:
SELECT BusinessEntityID, Bonus FROM Sales.SalesPerson WHERE Bonus = $0
变量不能同时包含这两个值。如果您需要这两个值,则可以定义两个变量来保存每列的值。
编辑:我不知道您的确切要求,所以从业务 这可能并不理想。但要给你一个摆脱的想法 您可以尝试以下SQL Server错误消息:
USE [AdventureWorks2012]
DECLARE @HighBonusAMT money, @LowBonusAMTnotzero money
DECLARE @NumberofSalespeopleBonusZero INT
DECLARE @NumberofSalespeopleGreaterthan4 INT
DECLARE @NumberofSalespeopleGreaterthanzerolessthanorequalto4 INT
DECLARE @NumberofSalespeopleBonusZeroID INT
DECLARE @NumberofSalespeopleGreaterthan4ID INT
DECLARE @NumberofSalespeopleGreaterthanzerolessthanorequalto4ID INT
SET @HighBonusAMT = (SELECT MAX(Bonus) FROM Sales.SalesPerson);
SET @LowBonusAMTnotzero = (SELECT MIN(Bonus) FROM Sales.SalesPerson WHERE Bonus <> $0);
SET @NumberofSalespeopleBonusZero = SELECT TOP 1 Bonus FROM Sales.SalesPerson WHERE Bonus = $0
SET @NumberofSalespeopleGreaterthan4 = SELECT TOP 1 Bonus FROM Sales.SalesPerson WHERE Bonus > 4000
SET @NumberofSalespeopleGreaterthanzerolessthanorequalto4 = SELECT TOP 1 Bonus FROM Sales.SalesPerson WHERE Bonus > 0 AND Bonus <= 4000
SET @NumberofSalespeopleBonusZeroID = SELECT TOP 1 BusinessEntityID Sales.SalesPerson WHERE Bonus = $0
SET @NumberofSalespeopleGreaterthan4ID = SELECT TOP 1 BusinessEntityID FROM Sales.SalesPerson WHERE Bonus > 4000
SET @NumberofSalespeopleGreaterthanzerolessthanorequalto4ID = SELECT TOP 1 BusinessEntityID FROM Sales.SalesPerson WHERE Bonus > 0 AND Bonus <= 4000
希望这有帮助!!!
答案 1 :(得分:0)
您是否尝试将整数存储到最后三个SELECT?
添加COUNT
:
SET @NumberofSalespeopleBonusZero = (SELECT COUNT(*) FROM Sales.SalesPerson WHERE Bonus = $0);
......等等。
答案 2 :(得分:0)
如果我没有注意到,你需要最后3个变量中的销售人员数量。
你可以这样做:
USE [AdventureWorks2012]
DECLARE @HighBonusAMT money, @LowBonusAMTnotzero money;
DECLARE @NumberofSalespeopleBonusZero INT, @NumberofSalespeopleGreaterthan4 INT, @NumberofSalespeopleGreaterthanzerolessthanorequalto4 INT;
SET @HighBonusAMT = ( SELECT MAX(Bonus)
FROM Sales.SalesPerson )
SET @LowBonusAMTnotzero = ( SELECT MIN(Bonus)
FROM Sales.SalesPerson
WHERE Bonus <> $0 )
SET @NumberofSalespeopleBonusZero = ( SELECT COUNT(*)
FROM Sales.SalesPerson
WHERE Bonus = $0
GROUP BY BusinessEntityID )
SET @NumberofSalespeopleGreaterthan4 = ( SELECT COUNT(*)
FROM Sales.SalesPerson
WHERE Bonus > 4000
GROUP BY BusinessEntityID )
SET @NumberofSalespeopleGreaterthanzerolessthanorequalto4 = ( SELECT COUNT(*)
FROM Sales.SalesPerson
WHERE Bonus > 0 AND Bonus <= 4000
GROUP BY BusinessEntityID )
答案 3 :(得分:0)
您还可以使用聚合函数在聚合中使用CASE
子句在一个语句中填充多个变量,例如:
USE [AdventureWorks2012]
DECLARE @HighBonusAMT money, @LowBonusAMTnotzero money;
DECLARE @NumberofSalespeopleBonusZero INT, @NumberofSalespeopleGreaterthan4 INT, @NumberofSalespeopleGreaterthanzerolessthanorequalto4 INT;
SELECT
@HighBonusAMT = MAX(Bonus)
,@LowBonusAMTnotzero = MIN(CASE WHEN Bonus > 0 THEN Bonus END)
,@NumberofSalespeopleBonusZero = COUNT(CASE WHEN Bonus = 0 THEN Bonus END)
,@NumberofSalespeopleGreaterthan4 = COUNT(CASE WHEN Bonus > 4000 THEN Bonus END)
,@NumberofSalespeopleGreaterthanzerolessthanorequalto4 = COUNT(CASE WHEN Bonus BETWEEN 0 AND 4000 THEN Bonus END)
FROM
Sales.SalesPerson
答案 4 :(得分:0)
对变量赋值后,会给出错误,因为在使用标量变量时,只能为变量赋值。您正在使用的子查询返回多个值。如果您要查找符合筛选条件的计数,请使用适当的函数并按需分组。如果您按businessentityid进行分组,并且如果您有多个businessentityid,则有可能获得表结果集(这意味着返回多行)。如果要计算与使用的过滤器匹配的每个businessentityId,则可以将变量声明为表变量而不是标量变量。希望这有用!!
SET @NumberofSalespeopleBonusZero =(SELECT BusinessEntityID FROM Sales.SalesPerson WHERE Bonus = $ 0); SET @ NumberofSalespeopleGreaterthan4 =(选择BusinessEntityID FROM Sales.SalesPerson WHERE Bonus&gt; 4000); SET @ NumberofSalespeopleGreaterthanzerolessthanorequalto4 =(选择BusinessEntityID FROM Sales.SalesPerson WHERE Bonus&gt; 0 AND Bonus&lt; = 4000);