【问题标题】:When i give Mulitiple values for @GetCustomerTable i got this error当我为 @GetCustomerTable 提供多个值时,出现此错误
【发布时间】:2016-01-08 10:12:26
【问题描述】:

消息 512,级别 16,状态 1,过程 GetNews2,第 27 行子查询 返回超过 1 个值。子查询时不允许这样做 遵循 =、!=、、>= 或当子查询用作 表达。消息 512,级别 16,状态 1,过程 GetNews2,第 32 行 子查询返回超过 1 个值。这是不允许的,当 子查询遵循 =、!=、、>= 或当子查询用作 一种表达。消息 512,级别 16,状态 1,过程 GetNews2,第 36 行 子查询返回超过 1 个值。这是不允许的,当 子查询跟在 =, !=, , , >= 之后,或者当 子查询用作表达式。消息 512,级别 16,状态 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

【问题讨论】:

  • 由于您是 SO 新手,请允许我提示:请注意,所有在 SO 上提供答案的专业人士都渴望获得声誉积分。请阅读this: someone-answers。谢谢!

标签: sql-server-2008


【解决方案1】:

如果您的类型变量 @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,则必须有一个特定的标量值...

【讨论】:

    猜你喜欢
    • 2020-02-10
    • 1970-01-01
    • 2019-08-22
    • 2021-12-28
    • 2011-05-11
    • 2022-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多