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 に答える