1

有没有一种方法可以执行条件连接,如下所示:

   CREATE TABLE #Entity 
   (    
    [AutoID] [int],
    [Code] [nvarchar](50) NOT NULL,
   )

    INSERT #Entity
    EXEC Entity_GetEntitiesByUserId @UserID

    DECLARE @Condition bit = 0

    SELECT * FROM [Stuff] s

       IF @Condition = 1 BEGIN

       INNER JOIN 
       (
        SELECT Code as eCode from
        #Entity
       ) e
        ON E.eCode  = s.EntityCode 

       END

       WHERE DeletedBy IS NULL

谢谢。

4

1 回答 1

7

这将在逻辑上做你想要的:

SELECT * 
FROM [Stuff] s
WHERE 
 DeletedBy IS NULL 
 and (@Condition = 0 or s.EntityCode in (select E.code from #Entitye))
于 2012-04-19T07:43:48.713 回答