To answer your questions, the base table contains millions of records but the query results contains only 3 records, so I inserted those three records into a temp table. ...
You need to declare the variables after the 'go' statement. The 'go' statement signals the end of a sql batch and closes the scope for those variables. If you move...