SQL Server子查询返回了多个值。子查询跟随=,!=,<,< =,>,> =或当子查询用作表达式时

时间:2013-08-14 22:06:58

标签: sql subquery

你好我们是在数据库sql上创建一个新系统然后我得到这个错误

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

这是一个查询

Declare @Weapon INT = (Select ItemID From SRO_VT_SHARD.dbo._Inventory WITH (Nolock)      Where CharID = @CharID 
AND Slot in (Select Slot From SRO_VT_SHARD.dbo._Inventory Where CharID = @CharID And Slot   Between '6' And '7'))

Declare @WeaponType3 Tinyint =
 (Select Typeid3 from SRO_VT_SHARD.dbo._RefObjCommon where ID in 
 (Select RefItemID from SRO_VT_SHARD.dbo._Items where ID64=@Weapon))

Declare @WeaponType4 Tinyint =
(Select Typeid4 from SRO_VT_SHARD.dbo._RefObjCommon where ID = 
(Select RefItemID from SRO_VT_SHARD.dbo._Items where ID64=@Weapon))

declare @RefWeapon int = (Select top 1 ID from SRO_VT_SHARD.dbo._RefObjCommon WITH (Nolock) where 
                    Service = 1 AND
                    TypeID1=3 AND --- Weapon
                    TypeID2=1 AND
                    TypeID3=@WeaponType3 AND
                    TypeID4=@WeaponType4
                    Group by SRO_VT_SHARD.dbo._RefObjCommon.ID,SRO_VT_SHARD.dbo._RefObjCommon.reqlevel1
                    having  (MAX(ReqLevel1)<=(Select CurLevel from SRO_VT_SHARD.dbo._Char where charid = @CharID))
                    Order By ReqLevel1 Desc 
               )
if (@RefWeapon is not null) And (@Weapon is not null) And (@WeaponType3 is not null)     And (@WeaponType4 is not null)
begin
               Update SRO_VT_SHARD.dbo._Items set RefItemID= @refweapon where ID64=     @Weapon
end

我需要修复此问题

2 个答案:

答案 0 :(得分:0)

好的,如果我没错,你在这里得到你的错误代码

declare @RefWeapon int = (Select top 1 ID from SRO_VT_SHARD.dbo._RefObjCommon WITH (Nolock) where 
                    Service = 1 AND
                    TypeID1=3 AND --- Weapon
                    TypeID2=1 AND
                    TypeID3=@WeaponType3 AND
                    TypeID4=@WeaponType4
                    Group by SRO_VT_SHARD.dbo._RefObjCommon.ID,SRO_VT_SHARD.dbo._RefObjCommon.reqlevel1
                    having  (MAX(ReqLevel1)<=(Select CurLevel from SRO_VT_SHARD.dbo._Char where charid = @CharID))
                    Order By ReqLevel1 Desc 
               )

特别在这部分

having  (MAX(ReqLevel1)<=(Select CurLevel from SRO_VT_SHARD.dbo._Char where charid = @CharID))

为什么呢?好的,MAX(ReqLevel1)是1值,但(Select CurLevel from SRO_VT_SHARD.dbo._Char where charid = @CharID)会返回很多值。所以你无法比较它们

您可能应该使用

(Select TOP 1 CurLevel from SRO_VT_SHARD.dbo._Char where charid = @CharID)

抱歉我的英语不好。希望这有帮助!

答案 1 :(得分:0)

这里的问题是子查询返回多行: SELECT TOP 1,或者您可以将您的=切换为IN并返回多行

示例:

SELECT t.ParentId,t1.parentname as parentname, t.childid as childid, 
   (select TOP 1 table1.childid from table1 where table1 .childname=table2.childid) as childname
FROM table1 t
left JOIN table2 t1 ON t.ParentId = t1.childid

---&gt;在这里使用你的代码