AS SELECT DISTINCT Variables_1.VarID, 
StatementReference_1.bRead AS VariableReferenceType, 
StatementReference_2.ResourceType, 
StatementReference_2.ResourceID, 
StatementReference_2.bRead AS ResourceReferenceType, 
Statements.Description AS StatementType, 
OccurrencesStmt.StartRow, 
Paths.PathStr, 
ProgramAliases.AliasName AS ProgramName, 
ProgramTypes.ProgramType,
Programs.ProgramID,
OccurrencesStmt.OccurID
FROM ((((((
	StatementReference AS StatementReference_1 
	INNER JOIN Variables AS Variables_1 ON StatementReference_1.ResourceID = Variables_1.VarID) 
	INNER JOIN StatementReference AS StatementReference_2 ON StatementReference_1.OccurID = StatementReference_2.OccurID AND 
                 StatementReference_1.bRead <> StatementReference_2.bRead) 
	INNER JOIN Statements ON StatementReference_1.StatementType = Statements.StatementType) 
	INNER JOIN OccurrencesStmt ON StatementReference_1.OccurID = OccurrencesStmt.OccurID) 
	INNER JOIN Paths ON OccurrencesStmt.PathID = Paths.PathID) 
	INNER JOIN Programs ON OccurrencesStmt.ProgID = Programs.ProgramID
	INNER JOIN ProgramAliases ON ProgramAliases.ProgramId = Programs.ProgramID and ProgramAliases.AliasType = 0)  
	INNER JOIN ProgramTypes ON Programs.ProgramTypeID = ProgramTypes.ProgramTypeID
WHERE (
	((StatementReference_1.ResourceType)=4)
	AND ((StatementReference_2.ResourceType) In (1,6,9,18,19,20,21,56,61,62,69,70,77,78,81,82,182,189,190,193, 214)) 
	AND (Variables_1.VarID IN (Select * from #numeric_param_temp))
	AND (StatementReference_1.bRead = 2) 
	AND (StatementReference_2.bRead = 1)
)
UNION 
SELECT DISTINCT Variables_1.VarID, 
2 AS VariableReferenceType, 
1000 as ResourceType, 
Variables_1.VarID  as ResourceID, 
1 AS ResourceReferenceType, 
'Initial Value' AS StatementType, 
Occurrences.StartRow, 
Paths.PathStr, 
ProgramAliases.AliasName AS ProgramName, 
ProgramTypes.ProgramType,
Programs.ProgramID,
Occurrences.OccurID
FROM 
	 Variables AS Variables_1
	INNER JOIN Occurrences on Occurrences.OccurID = Variables_1.OccurID
	INNER JOIN Paths ON Occurrences.PathID = Paths.PathID 
	INNER JOIN Programs ON Occurrences.ProgID = Programs.ProgramID
	INNER JOIN ProgramAliases ON ProgramAliases.ProgramId = Programs.ProgramID and ProgramAliases.AliasType = 0 
	INNER JOIN ProgramTypes ON Programs.ProgramTypeID = ProgramTypes.ProgramTypeID
WHERE 
	szValues <> 'NULL'
	AND (Variables_1.VarID IN (Select * from #numeric_param_temp))
ORDER BY Variables_1.VarID