0

We use SQL Server 2008. I am building dynamic sql in code. I had new requirements that needed further filtering. It would have been pretty messy to modify the sql builder class, so, diligently following the Open Closed principle, I create a class that wrapped the sql in a CTE and then added my new where clause to it. It works great, but since my builder knows exactly the fields it needs, but my decorator class does not, I selected * from the CTE. All the reasons not to use "select *" that I have researched did not seem to apply to this situation.

The question for you sql experts, is it bad to do select * on a cte (like the following example)?

;with employeeName_cte as 
(
    select 
       FirstName
      ,LastName
    from Employee
)
select *
from EmployeeName_cte 
where
   FirstName = 'bob';
DanCaveman
  • 676
  • 1
  • 6
  • 22
  • No, that wouldn't be bad. – shawnt00 Aug 21 '15 at 19:10
  • This is likely to get closed because it is primarily opinion based and therefore off topic. This is a bit a grey area for selecting *. If your cte selects ONLY those columns that are needed in the final output it isn't really too big of a deal. Of course if your code is in a view you still have the same issue as any other view using select *. I have employed this approach however for some situations so that when the cte changes it trickles down to the final output. – Sean Lange Aug 21 '15 at 19:11

0 Answers0