alter proc proc_GetIcode
@Name varchar(30),
@ParentCode varchar(20)
as
select Icode from Item where [Name] =@Name and Parent_Code = (select b.Parent_Code as pcode from Item as b, TargetItem as a where b.parent_code = a.code and a.[Name] = @ParentCode)
goexec proc_GetIcode '人员流失率','下属情绪管理'编译过去了,但执行exec时报错:Server: Msg 512, Level 16, State 1, Procedure proc_GetIcode, Line 5
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.请问是咋回事呀???

解决方案 »

  1.   

    这个查询
    select b.Parent_Code as pcode from Item as b, TargetItem as a where b.parent_code = a.code and a.[Name] = @ParentCode
    返回的值不唯一.
      

  2.   

    alter proc proc_GetIcode 
    @Name varchar(30), 
    @ParentCode varchar(20) 
    as 
    select Icode from Item where [Name] =@Name and Parent_Code in (select b.Parent_Code as pcode from Item as b, TargetItem as a where b.parent_code = a.code and a.[Name] = @ParentCode) 
    go exec proc_GetIcode '人员流失率','下属情绪管理' 把Parent_Code=的等号改称in
      

  3.   


    --作如下修改可以解决问题,但不知道结果是否满足楼主的要求
    alter proc proc_GetIcode
    @Name varchar(30),
    @ParentCode varchar(20)
    as
    select Icode from Item where [Name] =@Name and Parent_Code in (select b.Parent_Code as pcode from Item as b, TargetItem as a where b.parent_code = a.code and a.[Name] = @ParentCode)
    go--或alter proc proc_GetIcode
    @Name varchar(30),
    @ParentCode varchar(20)
    as
    select Icode from Item where [Name] =@Name and Parent_Code = (select top 1 b.Parent_Code as pcode from Item as b, TargetItem as a where b.parent_code = a.code and a.[Name] = @ParentCode)
    go
      

  4.   

    alter proc proc_GetIcode 
    @Name varchar(30), 
    @ParentCode varchar(20) 
    as 
    select Icode from Item where [Name] =@Name and Parent_Code in (select b.Parent_Code as pcode from Item as b, TargetItem as a where b.parent_code = a.code and a.[Name] = @ParentCode) 
    go exec proc_GetIcode '人员流失率','下属情绪管理'