如何在单个查询中执行“ IN”和“ WHERE”子句

时间:2020-01-23 11:00:09

标签: sql sql-server

我有一个SQL Server select语句,该语句应该基于大小写

案例1 :当提供的用户角色ID为admin时,将选择所有数据

情况2 :除管理员角色外,select还将返回除管理员数据外的所有数据。

DECLARE @UserRoleID INT = 3
SELECT urp.id AS 'iD'
    ,canview AS 'read'
    ,cancreate AS 'create'
    ,canedit AS 'update'
    ,candelete AS 'delete'
    ,ur.description AS 'userRole'
    ,ch.description AS 'rightsToMenu'
    ,urp.creator AS 'creator'
    ,urp.datecreated AS 'dateCreated'
    ,urp.datemodified AS 'dateModified'
    ,urp.modifier AS 'modifier'
FROM userrolepermission urp
INNER JOIN userrole ur ON urp.userroleid = ur.id
INNER JOIN channel AS ch ON urp.channelid = ch.id
WHERE urp.UserRoleID IN (
        (
            CASE 
                WHEN @UserRoleID = 1
                    THEN ('2')
                ELSE CAST(@UserRoleID AS NVARCHAR)
                END
            -- tried this also
            WHERE (
                    CASE 
                        WHEN urp.userroleid = @UserRoleID
                            AND @UserRoleID > 1
                            THEN @UserRoleID
                        ELSE @UserRoleID
                        END IN (
                        SELECT ID
                        FROM UserRole(NOLOCK)
                        )
                    )
            )
        )

在此条件返回所有不需要的数据。

示例:

@UserRoleID = 1-然后检索所有数据,然后If @UserRoleID != 1然后检索特定的匹配记录。

5 个答案:

答案 0 :(得分:2)

您可以简单地使用动态SQL。

DECLARE @query NVARCHAR(MAX)
  SET @query='SELECT urp.id AS [iD]
,canview AS [read]
,cancreate AS [create]
,canedit AS [update]
,candelete AS [delete]
,ur.description AS [userRole]
,ch.description AS [rightsToMenu]
,urp.creator AS [creator]
,urp.datecreated AS [dateCreated]
,urp.datemodified AS [dateModified]
,urp.modifier AS [modifier]
     FROM userrolepermission urp
     INNER JOIN userrole ur ON urp.userroleid = ur.id
   INNER JOIN channel AS ch ON urp.channelid = ch.id '


       IF @UserRoleID=1
    BEGIN

      SET @query += ' your condition'
       END 
     ELSE 

     BEGIN 

     SET @query += ' your condition'
     end


   EXECUTE @query 

答案 1 :(得分:1)

您可以改用布尔逻辑:

where (@UserRoleID = 1) OR
      (@UserRoleID <> 1 AND UserRole = @UserRoleID);

答案 2 :(得分:0)

您可以尝试以下

WHERE (
        isnull(@UserRoleID, 0) = 1
        OR (
            isnull(@UserRoleID, 0) <> 1
            AND dbo.TableName.UserRole = @UserRoleID
            )
        )

答案 3 :(得分:0)

您可以使用IF ELSE逻辑。即使看起来更长,也看起来更干净。

DECLARE @UserRoleID INT =3

IF @UserRoleID = 1 -- admin
BEGIN

   SELECT    
                          urp.id             AS 'iD',   
                          canview            AS 'read',   
                          cancreate          AS 'create',   
                          canedit            AS 'update',   
                          candelete          AS 'delete',   
                          ur.description     AS 'userRole',   
                          ch.description     AS 'rightsToMenu',   
                          urp.creator        AS 'creator',   
                          urp.datecreated    AS 'dateCreated',   
                          urp.datemodified   AS 'dateModified',   
                          urp.modifier       AS 'modifier'   
               FROM       userrolepermission AS urp   
               INNER JOIN userrole           AS ur   
               ON         urp.userroleid = ur.id   
               INNER JOIN channel AS ch   
               ON         urp.channelid = ch.id 
END
ELSE
BEGIN

     SELECT    
                          urp.id             AS 'iD',   
                          canview            AS 'read',   
                          cancreate          AS 'create',   
                          canedit            AS 'update',   
                          candelete          AS 'delete',   
                          ur.description     AS 'userRole',   
                          ch.description     AS 'rightsToMenu',   
                          urp.creator        AS 'creator',   
                          urp.datecreated    AS 'dateCreated',   
                          urp.datemodified   AS 'dateModified',   
                          urp.modifier       AS 'modifier'   
               FROM       userrolepermission AS urp   
               INNER JOIN userrole           AS ur   
               ON         urp.userroleid = ur.id   
               INNER JOIN channel AS ch   
               ON         urp.channelid = ch.id 
           WHERE urp.UserRoleID = @UserRoleID 

END     

答案 4 :(得分:0)

使用了此代码...

 WHERE urp.UserRoleID IN((CASE WHEN @UserRoleID = 1 THEN ('2') ELSE CAST(@UserRoleID AS NVARCHAR) END))

UserRoleID = 3记录可以使该代码有用