Brajesh Kumar
Brajesh Kumar

Reputation: 1

case statement in where clause - SQL Server 2008

SELECT  
   PDADate, T.Merchandizer_ID, T.Merchandizer, Merchandizer_LoginName, 
   STORE_ID, STORE_CODE, STORE_NAME,
   ACCOUNT_ID, ACCOUNT_NAME, Account_Store_Format_Id, Account_Store_Format,
   StoreType_Id, StoreType, T.Listid, T.Listname, 
   T.TimeIn, T.TimeOut, T.PlannedDate, T.Reason, TaskCode, TotalTime 
FROM 
   [dbo].Report_RD_Coverage T 
INNER JOIN 
   #TempLocationH TL ON TL.LocationId=T.Location_Id
WHERE 
   CONVERT(Date, PDADate) Between (@Start_Date) AND Isnull(@End_Date, @CurrentDate)
   AND T.Account_Id IN 
       (SELECT 
            CASE WHEN @Account_Id IS NULL THEN T.Account_Id 
                 ELSE (SELECT * FROM UDF_SplitString(@Account_Id,',')) 
            END
       )    
   AND T.StoreType_Id IN 
       (SELECT 
            CASE WHEN @StoreType_Id IS NULL THEN T.StoreType_Id 
                   ELSE (SELECT * FROM UDF_SplitString(@StoreType_Id,',')) 
            END
       )
  AND T.Store_Id IN
      (SELECT 
           CASE WHEN @Store_Id IS NULL THEN T.Store_Id 
                  ELSE (SELECT * FROM UDF_SplitString(@Store_Id,',')) 
           END
      )

If @Account_Id, @StoreType_Id and @Store_Id are null the it should select all the ACCOUNT_ID, STORETYPE_ID and STORE_ID otherwise based on parameter value it should filter.

UDF_SplitString is the function to split up comma-separated strings, and its return value is a table like:

 - 1
 - 2
 - 3

I'm getting this error:

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

Upvotes: 0

Views: 206

Answers (3)

Brajesh Kumar
Brajesh Kumar

Reputation: 1

> This is the my right solultion........now its working correctly

CREATE TABLE #Store_Id (StoreID varchar(20))

    IF @Store_Id != '0'
    BEGIN 
        INSERT INTO #Store_Id
        SELECT data FROM UDF_SplitString(@Store_Id,',')     
    END
    ELSE
        BEGIN
            INSERT INTO #Store_Id
            SELECT '0'
    END

CREATE TABLE #StoreType_Id (StoreTypeID varchar(20))

    IF @StoreType_Id != '0'
    BEGIN 
        INSERT INTO #StoreType_Id
        SELECT data FROM UDF_SplitString(@StoreType_Id,',')     
    END
    ELSE
        BEGIN
            INSERT INTO #StoreType_Id
            SELECT '0'
    END 

CREATE TABLE #Account_Id (AccountID varchar(20))

    IF @Account_Id != '0'
    BEGIN 
        INSERT INTO #Account_Id
        SELECT data FROM UDF_SplitString(@Account_Id,',')       
    END
    ELSE
        BEGIN
            INSERT INTO #Account_Id
            SELECT '0'
    END     

INSERT INTO #FinalTable(VisitDate,Merchandizer_Id,Merchandizer,MerchandizerLogin,StoreId,StoreCode,StoreName,AccountId,AccountName,
    Account_Store_Format_Id,Account_Store_Format,StoreTypeId ,StoreType ,ListId ,ListName,TimeIn ,TimeOut,PlannedDate ,Reason ,TaskCode,TotalTime)      

SELECT  Visit_Date,T.Merchandizer_ID,T.Merchandizer,Merchandizer_LoginName,STORE_ID,STORE_CODE,STORE_NAME,ACCOUNT_ID,ACCOUNT_NAME,
        Account_Store_Format_Id,Account_Store_Format,StoreType_Id, 
        StoreType,T.Listid,T.Listname,T.TimeIn,T.TimeOut,T.PlannedDate,T.Reason,TaskCode,TotalTime 
FROM [dbo].Report_RD_Coverage T
INNER JOIN #TempLocationH TL ON TL.LocationId=T.Location_Id
INNER JOIN #Store_Id on CONVERT(VARCHAR,t.Store_Id) = CASE WHEN @Store_Id = '0' THEN convert(VARCHAR,t.Store_Id) ELSE StoreID END
INNER JOIN #StoreType_Id on CONVERT(VARCHAR,t.StoreType_Id) = CASE WHEN @StoreType_Id = '0' THEN convert(VARCHAR,t.StoreType_Id) ELSE StoreTypeID END
INNER JOIN #Account_Id on CONVERT(VARCHAR,t.Account_Id) = CASE WHEN @Account_Id = '0' THEN convert(VARCHAR,t.Account_Id) ELSE AccountID END

WHERE CONVERT(Date,PDADate) Between @Start_Date AND @End_Date

Upvotes: 0

Builder
Builder

Reputation: 1056

I tried this and reached very closer but you have to do something from what I found a link. This is my try. the only thing you need to build is the @udf data.

declare @Store_Id INT;
declare @Account_Id INT;
DECLARE @UDF[9] OF VARCHAR(30);
set @Store_Id = 99 --NULL  
set @Account_Id = 15  
SET @UDF = '11,12,13,14,15,16'

SELECT @Account_Id AS ACID
WHERE CAST(@Account_Id AS VARCHAR(6)) IN (
   CASE WHEN @Store_Id IS NULL THEN CAST(@Account_Id AS VARCHAR(6)) 
   ELSE @UDF  END 

The link is at http://www.codeproject.com/Questions/473174/CreateplusArrayplusinplusSqlplusServer

DECLARE @INSTR as VARCHAR(MAX)
SET @INSTR = '2,3,177,'
DECLARE @SEPERATOR as VARCHAR(1)
DECLARE @SP INT
DECLARE @VALUE VARCHAR(1000)
SET @SEPERATOR = ','
CREATE TABLE #tempTab (id int not null)
WHILE PATINDEX('%' + @SEPERATOR + '%', @INSTR ) <> 0 
BEGIN
   SELECT  @SP = PATINDEX('%' + @SEPERATOR + '%',@INSTR)
   SELECT  @VALUE = LEFT(@INSTR , @SP - 1)
   SELECT  @INSTR = STUFF(@INSTR, 1, @SP, '')
   INSERT INTO #tempTab (id) VALUES (@VALUE)
END
SELECT * FROM myTable WHERE id IN **(SELECT id FROM #tempTab)**
DROP TABLE #tempTab

you can extract for the sql in bold and the logic how to create temp table and its data and I hope you will get what you want.

Upvotes: 0

D&#39;Arcy Rittich
D&#39;Arcy Rittich

Reputation: 171421

CASE must return a scalar value, so try this variation instead:

select PDADate, T.Merchandizer_ID, T.Merchandizer, Merchandizer_LoginName, STORE_ID, STORE_CODE, 
    STORE_NAME, ACCOUNT_ID, ACCOUNT_NAME, Account_Store_Format_Id, Account_Store_Format, 
    StoreType_Id, StoreType, T.Listid, T.Listname, T.TimeIn, T.TimeOut, T.PlannedDate, 
    T.Reason, TaskCode, TotalTime
from [dbo].Report_RD_Coverage T
inner join #TempLocationH TL on TL.LocationId = T.Location_Id
where CONVERT(date, PDADate) between (@Start_Date)
        and Isnull(@End_Date, @CurrentDate)
    and (
        @Account_Id is null
        or T.Account_Id in (
            select *
            from UDF_SplitString(@Account_Id, ',')
            )
        )
    and (
        @StoreType_Id is null
        or T.StoreType_Id in (
            select *
            from UDF_SplitString(@StoreType_Id, ',')
            )
        )
    and (
        @Store_Id is null
        or T.Store_Id in (
            select *
            from UDF_SplitString(@Store_Id, ',')
            ) end
        )

Upvotes: 1

Related Questions