当我给@GetCustomerTable的Mulitiple值时,我得到了这个错误

时间:2016-01-08 10:12:27

标签: sql-server-2008

  

Msg 512,Level 16,State 1,Procedure GetNews2,Line 27 Subquery   返回超过1的值。子查询时不允许这样做   follow =,!=,<,< =,>,> =或者当子查询用作   表达。消息512,级别16,状态1,过程GetNews2,行32   子查询返回的值超过1。这是不允许的   子查询跟随=,!=,<,< =,>,> =或当子查询用作   一种表达。 Msg 512,Level 16,State 1,Procedure GetNews2,Line 36   子查询返回的值超过1。这是不允许的   子查询跟随=,!= ,,< =,>,> =或者当   子查询用作表达式。 Msg 512,Level 16,State 1,   过程GetNews2,第40行子查询返回的值超过1。这个   子查询遵循=,!=,<,< =,>,> =或者不允许   当子查询用作表达式时。

alter proc GetNews2
(@GetCustomerTable [dbo].[CustomerTableType] readonly,
@GetProjectTable [dbo].[ProjectTableType] readonly )
as
begin
set nocount on

CREATE table #newsTemp( NewsId      BIGINT,
                            Title       nchar(550),
                            [Description] NVARCHAR(max),
                            CreatedDate Datetimeoffset,
                            CreatedBy int,
                            IsDeleted bit,
                            CustomerId  INT,
                            Projectid   INT) 

;WITH Getnew_cte (NewsId,Title,[Description],CreatedDate,CreatedBy,IsDeleted,CustomerId,ProjectId)  
as
(
select N.NewsId,N.Title,N.[Description],N.CreatedDate,N.CreatedBy,N.IsDeleted,ct.CustomerId,pt.ProjectId  from News N
full join CustomersNews ct on N.NewsId = ct.NewsId 
full join ProjectsNews pt on N.NewsId = pt.NewsId 
)
 insert into #newsTemp
select NewsId,Title,[Description],CreatedDate,CreatedBy,IsDeleted,CustomerId,ProjectId from Getnew_cte

 if 
  ((select * from @GetCustomerTable) IS NULL and (select * from @GetProjectTable) is null)
  begin
  (select * from #newsTemp where #newsTemp.CustomerId Is nulL and #newsTemp.ProjectId is null )
  end
 if ((select * from @GetCustomerTable) is null) and ((select * from @GetProjectTable) is not null)
 begin
  (select * from #newsTemp where #newsTemp.CustomerId Is null and #newsTemp.ProjectId in (select projectId from @GetProjectTable))
  end
 if ((select *from @GetCustomerTable) is not null) and ((select * from @GetProjectTable) is null)
 begin
  (select * from #newsTemp where #newsTemp.CustomerId in (select CustomerId from @GetCustomerTable) and #newsTemp.ProjectId is null)
  end
 if ((select * from @GetCustomerTable) is not null) and ((select * from @GetProjectTable)  is not null)
 begin 
 (select * from #newsTemp where ((#newsTemp.CustomerId in (select CustomerId from @GetCustomerTable)) and (#newsTemp.ProjectId in (select projectId from @GetProjectTable))))
 end
 end

1 个答案:

答案 0 :(得分:0)

在您的类型变量@GetCustomerTable中使用多个值时,这将不起作用:

(select * from @GetCustomerTable) IS NULL

您可以尝试

(select count(*) from @GetCustomerTable)=0

但是 - 实际上 - 只有当您希望@GetCustomerTable为空时才会有效...

另一种方法可能是(但这取决于您的表类型的内容)

(select TOP 1 OneSpecificColumn FROM @GetCustomerTable) IS NULL

(select OneSpecificColumn FROM @GetCustomerTable WHERE aCritReturningOneRow) IS NULL

无论如何:如果你想查看IS NULL,必须有一个特定的标量值......