SELECT * INTO #transactionReportDetails FROM (SELECT
---------------------------------------
WHERE (@transactionDateTimeFromLocal IS NULL OR tt.transferDateTime >= @transactionDateTimeFromLocal) AND (@transactionDateTimeToLocal IS NULL OR tt.transferDateTime <= @transactionDateTimeToLocal)
AND (aaS.agentId = @agentId OR aD.agentId = @agentId)
AND (@networkOwnerId IS NULL OR td.networkOwnerAgentId = @networkOwnerId) ) AS tmp;