title: Replace redundant Code with SP / UDF
body: <p>I have code:</p>
  
  <pre><code>SELECT Date
  ,trafficsource
  INTO #temp
  FROM SomeTable;
  </code></pre>
  
  <p>That is the first step. The second step is the following code:</p>
  
  <pre><code>INSERT INTO dbo.DimTrafficSource ( TrafficSource, LogInsert )
  SELECT DISTINCT fact.trafficSource
  ,GETDATE()
  FROM #temp fact
  LEFT JOIN dbo.DimTrafficSource dim ON fact.trafficSource = dim.TrafficSource
  WHERE dim.TrafficSource IS NULL
  </code></pre>
  
  <p>Now, the second step is used in many other stored procedures. To avoid using the same "<code>hardcoded</code>" code in them, I'd prefer to replace this functionality with other function / stored procedure so when I want to modify the logic, I want to modify it at one place, not in every stored procedure using this code. Is it possible when there is a temp table used? How?</p>