Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 动态筛选表错误子查询返回的值超过1_Sql_Sql Server_Function_Return Value - Fatal编程技术网

Sql 动态筛选表错误子查询返回的值超过1

Sql 动态筛选表错误子查询返回的值超过1,sql,sql-server,function,return-value,Sql,Sql Server,Function,Return Value,我有一个过滤器在我的网站搜索。所以我需要得到一家公司的品牌名称,该公司在一年内有一个产品 我想输入一个以上的年份值或空值 我使用拆分函数返回年份表 declare @test nvarchar(50) = '1991,1997' select BrandName from Item where DisplayPrice =1 and YearMan in (CASE WHEN @test is not null THEN (select * from split(@test,',')) E

我有一个过滤器在我的网站搜索。所以我需要得到一家公司的品牌名称,该公司在一年内有一个产品

我想输入一个以上的年份值或空值

我使用拆分函数返回年份表

declare @test nvarchar(50) = '1991,1997'


select BrandName from Item where DisplayPrice =1
 and YearMan in (CASE WHEN @test is not null THEN (select * from split(@test,',')) ELSE YearMan END) 
分裂函数

ALTER FUNCTION [dbo].[Split](
    @sInputList VARCHAR(8000) -- List of delimited items
  , @sDelimiter VARCHAR(8000) = ',' -- delimiter that separates items
) RETURNS @List TABLE (item VARCHAR(8000))

BEGIN

DECLARE @sItem VARCHAR(8000)


    WHILE CHARINDEX(@sDelimiter,@sInputList,0) <> 0
     BEGIN
         SELECT
          @sItem=RTRIM(LTRIM(SUBSTRING(@sInputList,1,CHARINDEX(@sDelimiter,@sInputList,0)-1))),
          @sInputList=RTRIM(LTRIM(SUBSTRING(@sInputList,CHARINDEX(@sDelimiter,@sInputList,0)+LEN(@sDelimiter),LEN(@sInputList))))

         IF LEN(@sItem) > 0
          INSERT INTO @List SELECT @sItem
     END

        IF LEN(@sInputList) > 0
         INSERT INTO @List SELECT @sInputList -- Put the last item in
RETURN
ALTER函数[dbo].[Split](
@sInputList VARCHAR(8000)——分隔项列表
,@sDelimiter VARCHAR(8000)=',--分隔项的分隔符
)返回@List表(项VARCHAR(8000))
开始
声明@sItem VARCHAR(8000)
而CHARINDEX(@sDelimiter,@sInputList,0)0
开始
挑选
@sItem=RTRIM(LTRIM(子字符串(@sInputList,1,CHARINDEX(@sDelimiter,@sInputList,0)-1)),
@sInputList=RTRIM(LTRIM(子字符串(@sInputList,CHARINDEX(@sDelimiter,@sInputList,0)+LEN(@sDelimiter),LEN(@sInputList)))
如果LEN(@sItem)>0
插入@List选择@sItem
结束
如果LEN(@sInputList)>0
插入@List选择@sInputList——将最后一项放入
返回

结束

您可以这样重写它:

select BrandName
from Item
where DisplayPrice =1
 and (@test IS NULL OR YearMan in (select item from split(@test,','))
declare @test nvarchar(50) = '1991,1997'

select  BrandName 
from    Item 
where   DisplayPrice =1
and     @test is not null 
and     YearMan in (select * from dbo.split(@test,','))
union all
select  BrandName 
from    Item 
where   DisplayPrice =1
and     @test is null 

-- or

declare @test nvarchar(50) = '1991,1997'
declare @array table (yyyy smallint primary key);
insert  @array (yyyy)
select  * from dbo.split(@test,',');

select  BrandName 
from    Item 
where   DisplayPrice =1
and     @test is not null 
and     YearMan in (select yyyy from @array)
union all
select  BrandName 
from    Item 
where   DisplayPrice =1
and     @test is null