在动态查询中将变量用作INNER JOIN

时间:2018-10-16 22:52:48

标签: sql sql-server tsql

我有一个要用于动态存储过程的表,

  --Creating table
 DECLARE @SearchKeys AS TABLE(
             DesignKey INT
            )
  --Filling table
 SELECT  pd.DesignKey  FROM [Project] AS [p]
                           LEFT JOIN [ProjectDesign] AS [pd] ON [p].[ProjectKey] = [pd].[ProjectKey]
                           LEFT JOIN [Design] AS [d] ON [pd].[DesignKey] = [d].[DesignKey]
                           INNER JOIN [DesignType] AS [DE] ON [D].
                            WHERE JN.Note LIKE '%'+@JobNote+'%'
                            GROUP BY ParentGuid) JN ON P.ProjectGuid = JN.ParentGuid

现在我的桌子上有多个DesignKeys

现在在我的动态查询中,我想对该表进行内部联接,如下所示:

   DECLARE @Sql NVARCHAR(MAX) = '
            DECLARE @DList TABLE
            (
                            [ProjectKey]         INT
                           , [Project Name]       NVARCHAR(255)
                           ...
            )
   INSERT INTO @DList
                 SELECT
                       [p].[ProjectKey]
                     , [p].[Name] AS [Project Name]
                     ....
                  FROM [Project] AS [p]
                  INNER JOIN '+@SearchKeys+' AS [SK] ON [D].[DesignKey] = [SK].[DesignKey]

但是@SearchKeys变量用红色标记并抛出

  

必须声明标量变量“ @SearchKeys”

但是我已经在查询中声明了它。我在做什么错了?

完整代码:

               DECLARE @Params NVARCHAR(MAX) = '
      @LastName AS        NVARCHAR(255)
    , @ProjName         NVARCHAR(100)       
    , @RegAbbrv         NVARCHAR(4)  
    , @ProjNum          NVARCHAR(6)  
    , @PostalCode       NVARCHAR(10) 
    , @City             NVARCHAR(255)
    , @AddressLine      NVARCHAR(255)
    , @SortBy       INT          
    , @IncludeArchived  BIT    
    , @IncludeVoided     BIT      
    , @Status           NVARCHAR(255)
    , @JobNote AS       NVARCHAR(255)
    , @Folio   AS        NVARCHAR(255)
    , @DesignType AS      NVARCHAR(255)
    , @Contact AS        NVARCHAR(255)
    , @Customer AS       NVARCHAR(255)
                                '


        DECLARE @SearchKeys AS TABLE(
             DesignKey INT
            )

            INSERT INTO @SearchKeys
            SELECT  pd.DesignKey  FROM [Project] AS [p]
                           LEFT JOIN [ProjectDesign] AS [pd] ON [p].[ProjectKey] = [pd].[ProjectKey]
                           LEFT JOIN [Design] AS [d] ON [pd].[DesignKey] = [d].[DesignKey]
                           INNER JOIN [DesignType] AS [DE] ON [D].[DesignTypeGuid] = [DE].[DesignTypeGuid]
                           LEFT JOIN [General] AS [g] ON [g].[GeneralKey] = [d].[StatusKey]
                           LEFT JOIN [General] AS [h] ON [h].[GeneralKey] = [d].[ShopsStatus]
                           INNER JOIN [Address] AS [a] ON [a].[AddressGuid] = [p].[ProjectGuid]
                           INNER JOIN [Region] AS [re] ON [a].[RegionKey] = [re].[RegionKey]
                           INNER JOIN (SELECT ParentGuid FROM JobNotes JN WITH (NOLOCK) 
                            WHERE JN.Note LIKE '%'+@JobNote+'%'
                            GROUP BY ParentGuid) JN ON P.ProjectGuid = JN.ParentGuid
   DECLARE @Sql NVARCHAR(MAX) = '
            DECLARE @DList TABLE
            (
                            [ProjectKey]         INT
                           , [Project Name]       NVARCHAR(255)
                           , [Project Number]     NVARCHAR(12)
                           , [Design Status]      NVARCHAR(255)
                           , [Design Locked]      BIT
                           , [DesignKey]          INT
                           , [Design Name]        NVARCHAR(255)
                           , [DesignItemName]     NVARCHAR(255)
                           , [DesignNumber]       INT
                           , [RevisionNumber]     INT
                           , [Folio]              INT
                           , [Design Created]     DATETIME
                           , [StatusKey]          INT
                           , [ReportKey]          INT
                           , [ReportSubKey]       INT
                           , [Abbrv]              NVARCHAR(255)
                           , [Archived]           BIT
                           , [ProjGuid]           UNIQUEIDENTIFIER
                           , [RunCount]           INT
                           , [RunUnconCnt]        INT
                           , [ShopsStatus]        INT
                           , [HasAddress]         BIT
                           , [ShopStatusExtended] NVARCHAR(255)
                           , [RegionKey]          INT
                           , [IsLocked]           BIT
                           , [DesignTypeName]     NVARCHAR(255)
                           , [DueDate]            DATETIME
                           , [IsDeleted]          BIT 
            )
            INSERT INTO @DList
                 SELECT
                       [p].[ProjectKey]
                     , [p].[Name] AS [Project Name]
                     , [p].[LegacyKey] AS [Project Number]
                     , [g].[Label] AS [Design Status]
                     , CASE [g].[Label]
                          WHEN ''Dead''
                          THEN 1
                          ELSE 0
                       END AS [Design Locked]
                     , [pd].[DesignKey]
                     , [d].[Name] AS [Design Name]
                     , CONCAT([DE].[Abbreviation] , '' '' , [D].[DesignNumber] ,
                                                CASE
                                                   WHEN [D].[RevisionNumber] IS NULL
                                                   THEN ''''
                                                   ELSE CONCAT('' Rev '' , [D].[RevisionNumber])
                                                END) AS [DesignItemName]
                     , [d].[DesignNumber]
                     , [d].[RevisionNumber]
                     , [d].[Folio]
                     , [d].[tsCreated] AS [Design Created]
                     , [g].[GeneralKey] AS [StatusKey]
                     , [p].[ReportKey]
                     , [p].[ReportSubKey]
                     , [re].[Abbrv]
                     , [p].[Archived]
                     , [p].[ProjectGuid]
                     , (SELECT
                             COUNT([RunKey])
                             FROM [Run]
                             WHERE [DesignKey] = [d].[DesignKey]) AS [RunCnt]
                     , (SELECT
                             COUNT([RunGUID])
                             FROM [RunsUnconsolidated]
                             WHERE [DesignKey] = [d].[DesignKey]) AS [RunUnconCnt]
                     , [d].[ShopsStatus]
                     , CASE
                          WHEN([a].[AddressLine1] = ''Not available''
                              OR [a].[PostalCode] = ''00000'')
                          THEN 0
                          ELSE 1
                       END AS [HasAddress]
                     , [h].[Extended] AS [ShopStatusExtended]
                     , [a].[RegionKey]
                     , [d].[IsLocked]
                     , [DE].[Name]
                     , [P].[BidDueDate] AS [DueDate]
                     , [D].[IsDeleted] AS [IsDeleted]
                       FROM [Project] AS [p]
                           LEFT JOIN [ProjectDesign] AS [pd] ON [p].[ProjectKey] = [pd].[ProjectKey]
                           LEFT JOIN [Design] AS [d] ON [pd].[DesignKey] = [d].[DesignKey]
                           INNER JOIN [DesignType] AS [DE] ON [D].[DesignTypeGuid] = [DE].[DesignTypeGuid]
                           LEFT JOIN [General] AS [g] ON [g].[GeneralKey] = [d].[StatusKey]
                           LEFT JOIN [General] AS [h] ON [h].[GeneralKey] = [d].[ShopsStatus]
                           INNER JOIN [Address] AS [a] ON [a].[AddressGuid] = [p].[ProjectGuid]
                           INNER JOIN [Region] AS [re] ON [a].[RegionKey] = [re].[RegionKey]
                           INNER JOIN '+@SearchKeys+' AS [SK] ON [D].[DesignKey] = [SK].[DesignKey]


 WHERE
                       ((@ProjName = ''%'')
                            OR ([p].[Name] LIKE(''%''+@ProjName +''%''))
                            OR ([p].[LegacyKey] LIKE(@ProjNum)
                               AND [p].[LegacyKey] LIKE(''%''+@ProjName+''%'')))
                           AND [re].[Abbrv] LIKE(@RegAbbrv)
                           AND ([p].[LegacyKey] = ''%''
                               OR [p].[Legacykey] LIKE(@ProjNum))
                           AND [a].[PostalCode] LIKE(@PostalCode)
                           AND [a].[City] LIKE(@City)
                           AND [a].[AddressLine1] LIKE(@AddressLine)
                           AND (@Status = ''%''
                               OR ([D].[IsLocked] = 0
                                  AND [g].[Label] LIKE(@Status)))
                           AND (@IncludeArchived = 1
                               OR [Archived] = 0)
                             AND (@IncludeVoided = 1
                               OR [D].[IsDeleted] = 0)
                           AND (@Folio = ''%''
                             OR [D].[Folio] LIKE(@Folio))
                           AND ([DE].[Abbreviation] = ''%''
                               OR [DE].[Abbreviation] LIKE(@DesignType))
   ';
            DECLARE @Select NVARCHAR(MAX) = ' SELECT
                    *
                    FROM @DList
               ORDER BY
                    '+@CurrentSortBy+'
                    , [Design Name]
                    , [DesignTypeName]
                    , [DesignNumber]
                    , [RevisionNumber]
                            ';
            DECLARE @Query NVARCHAR(MAX) = @Sql + @Select
            EXEC [sp_executesql]
                @Query ,
                @Params ,
                @LastName ,
                @ProjName ,
                @RegAbbrv ,
                @ProjNum ,
                @PostalCode ,
                @City ,
                @AddressLine ,
                @SortBy ,
                @IncludeArchived ,
                @IncludeVoided ,
                @Status ,
                @JobNote ,
                @Folio ,
                @DesignType ,
                @Contact ,
                @Customer

1 个答案:

答案 0 :(得分:1)

表变量未在exec中继承。但是,临时表是。所以我建议:

CREATE TABLE #SearchKeys (
    DesignKey INT
);

 . . . 

DECLARE @Sql NVARCHAR(MAX) = '
DECLARE @DList TABLE (
    [ProjectKey] INT,
    [Project Name] NVARCHAR(255)
    ...
   );
INSERT INTO @DList ( . . . )
    SELECT [p].[ProjectKey], [p].[Name] AS [Project Name], ....
    FROM [Project] [p] JOIN
         #SearchKeys [SK]
         ON [D].[DesignKey] = [SK].[DesignKey]
'