Skip to main content
bug fixed
Source Link
brz
  • 53
  • 6

please try this query built with sp_executesql

declare @tableName nvarchar(100) = 'evt_scan_20171222';
declare @SQLString nvarchar(1001000)
SET @SQLString =  N'
    SELECT S.ScanID [Scan ID], S.PolicyName [Scan], 
        DATEADD(hh, +2, S.StartTime) [Scan Start], A.AssetName [Scanner], 
        COUNT(AIPS.AssetIPID) [Hosts], S.TransactionGroup [Transaction Group ID]
    FROM Scan S (NOLOCK)
        LEFT JOIN Scanner SC (NOLOCK) ON S.ScannerID = SC.ScannerID
        LEFT JOIN Asset A (NOLOCK) ON SC.AssetID = A.AssetID
        LEFT JOIN Asset_IP_Scan AIPS (NOLOCK) ON S.ScanID = AIPS.ScanID
        LEFT JOIN ' + @tableName + ' evt1 ON S.TransactionGroup = evt1.evtTransactionGroup
    WHERE S.ProcessStatus < 2
    GROUP BY S.ScanID, S.PolicyName, S.StartTime, A.AssetName, S.TransactionGroup
    ORDER BY S.StartTime ASC, S.PolicyName ASC, A.AssetName ASC'
execute sp_executesql @sqlstring

But actually dynamically tables names is not best practice.

Update

Thanks to Aaron Bertrand in comments. Changes:

declare @SQLString nvarchar(100) //- not enough for hande sql string.

please try this query built with sp_executesql

declare @tableName nvarchar(100) = 'evt_scan_20171222';
declare @SQLString nvarchar(100)
SET @SQLString =  N'
    SELECT S.ScanID [Scan ID], S.PolicyName [Scan], 
        DATEADD(hh, +2, S.StartTime) [Scan Start], A.AssetName [Scanner], 
        COUNT(AIPS.AssetIPID) [Hosts], S.TransactionGroup [Transaction Group ID]
    FROM Scan S (NOLOCK)
        LEFT JOIN Scanner SC (NOLOCK) ON S.ScannerID = SC.ScannerID
        LEFT JOIN Asset A (NOLOCK) ON SC.AssetID = A.AssetID
        LEFT JOIN Asset_IP_Scan AIPS (NOLOCK) ON S.ScanID = AIPS.ScanID
        LEFT JOIN ' + @tableName + ' evt1 ON S.TransactionGroup = evt1.evtTransactionGroup
    WHERE S.ProcessStatus < 2
    GROUP BY S.ScanID, S.PolicyName, S.StartTime, A.AssetName, S.TransactionGroup
    ORDER BY S.StartTime ASC, S.PolicyName ASC, A.AssetName ASC'
execute sp_executesql @sqlstring

But actually dynamically tables names is not best practice.

please try this query built with sp_executesql

declare @tableName nvarchar(100) = 'evt_scan_20171222';
declare @SQLString nvarchar(1000)
SET @SQLString =  N'
    SELECT S.ScanID [Scan ID], S.PolicyName [Scan], 
        DATEADD(hh, +2, S.StartTime) [Scan Start], A.AssetName [Scanner], 
        COUNT(AIPS.AssetIPID) [Hosts], S.TransactionGroup [Transaction Group ID]
    FROM Scan S (NOLOCK)
        LEFT JOIN Scanner SC (NOLOCK) ON S.ScannerID = SC.ScannerID
        LEFT JOIN Asset A (NOLOCK) ON SC.AssetID = A.AssetID
        LEFT JOIN Asset_IP_Scan AIPS (NOLOCK) ON S.ScanID = AIPS.ScanID
        LEFT JOIN ' + @tableName + ' evt1 ON S.TransactionGroup = evt1.evtTransactionGroup
    WHERE S.ProcessStatus < 2
    GROUP BY S.ScanID, S.PolicyName, S.StartTime, A.AssetName, S.TransactionGroup
    ORDER BY S.StartTime ASC, S.PolicyName ASC, A.AssetName ASC'
execute sp_executesql @sqlstring

But actually dynamically tables names is not best practice.

Update

Thanks to Aaron Bertrand in comments. Changes:

declare @SQLString nvarchar(100) //- not enough for hande sql string.
Source Link
brz
  • 53
  • 6

please try this query built with sp_executesql

declare @tableName nvarchar(100) = 'evt_scan_20171222';
declare @SQLString nvarchar(100)
SET @SQLString =  N'
    SELECT S.ScanID [Scan ID], S.PolicyName [Scan], 
        DATEADD(hh, +2, S.StartTime) [Scan Start], A.AssetName [Scanner], 
        COUNT(AIPS.AssetIPID) [Hosts], S.TransactionGroup [Transaction Group ID]
    FROM Scan S (NOLOCK)
        LEFT JOIN Scanner SC (NOLOCK) ON S.ScannerID = SC.ScannerID
        LEFT JOIN Asset A (NOLOCK) ON SC.AssetID = A.AssetID
        LEFT JOIN Asset_IP_Scan AIPS (NOLOCK) ON S.ScanID = AIPS.ScanID
        LEFT JOIN ' + @tableName + ' evt1 ON S.TransactionGroup = evt1.evtTransactionGroup
    WHERE S.ProcessStatus < 2
    GROUP BY S.ScanID, S.PolicyName, S.StartTime, A.AssetName, S.TransactionGroup
    ORDER BY S.StartTime ASC, S.PolicyName ASC, A.AssetName ASC'
execute sp_executesql @sqlstring

But actually dynamically tables names is not best practice.