简体   繁体   English

在INNER JOIN子句中使用OR

[英]Using OR into INNER JOIN clause

I have a dynamic query where I declare 3 different variables like: 我有一个动态查询,在其中声明3个不同的变量,例如:

       DECLARE @JobNoteQuery NVARCHAR(MAX) = IIF(@JobNote = '%' , '' , ' INNER JOIN (SELECT ParentGuid FROM JobNotes JN WITH (NOLOCK) 
                            WHERE JN.Note LIKE ''%''+@JobNote+ ''%''
                            GROUP BY ParentGuid) JN ON P.ProjectGuid = JN.ParentGuid');

       DECLARE @ContactQuery NVARCHAR(MAX) = IIF(@Contact = '%' , '' , ' LEFT JOIN(SELECT
                        [ProjectKey]
                        FROM [ProjectCustomerContact] AS [pcc]
                            LEFT JOIN [Contact] AS [c] ON [c].[ContactKey] = [pcc].[ContactKey]
                        WHERE [c].[LastName] LIKE ''%''+@LastName+''%'') AS [pcc] ON [pcc].[ProjectKey] = [p].[ProjectKey]')

       DECLARE @CustomerQuery NVARCHAR(MAX) = IIF(@Customer = '%' , '' , 'LEFT JOIN(SELECT
                        [ProjectKey]
                        FROM [ProjectCustomer] AS [pc]
                            LEFT JOIN [Customer] AS [c] ON [c].[CustomerKey] = [pc].[CustomerKey]
                        WHERE([c].[Name] LIKE ''%''+@CustName+''%''
                            OR [c].[DBA] LIKE ''%''+@CustName+''%'')) AS [PC] ON [pc].[ProjectKey] = [p].[ProjectKey]')

So into select query I use that variables into Joins clause like: 因此,在选择查询中,我将该变量用于Joins子句中,例如:

  ...INNER JOIN [Region] AS [re] ON [a].[RegionKey] = [re].[RegionKey]
                          '+@JobNoteQuery+'
                          '+@ContactQuery+'
                          '+@CustomerQuery+'
                       WHERE...

My question is. 我的问题是。 If my tree parameters return their values I mean @JobNoteQuery , @ContactQuery and @CustomerQuery , always return inners and left joins, but value can be null if one of them doesn't have value it just will return empty list, so there is any way to use OR clause into INNER JOINS ? 如果我的树形参数返回它们的值,我的意思是@JobNoteQuery@ContactQuery@CustomerQuery ,总是返回内部连接和左连接,但是如果其中一个不具有值,则值可以为null,它将返回空列表,因此有在内部INNER JOINS使用OR子句的方法? Like: 喜欢:

 ...INNER JOIN [Region] AS [re] ON [a].[RegionKey] = [re].[RegionKey]
          OR
                              '+@JobNoteQuery+'
          OR
                              '+@ContactQuery+'
          OR
                              '+@CustomerQuery+'
                           WHERE...

That's not possible? 那不可能吗 Regards 问候

insert your data into temp tables and then you can either start using Unions provided to have same column data types to just get a clubbed result-set of each of the 3 queries. 将数据插入临时表中,然后您就可以开始使用提供的具有相同列数据类型的并集来获取3个查询中每个查询的组合结果集。 OR you have data in temp tables putting desired join condition be left or inner joins will be a trivial matter at that point. 或者,您在临时表中保留了所需的联接条件的数据,否则内部联接在那时将是一件小事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM