0

Views:

create view EMP as
select e.EmployeeID, e.FirstName from Employees e

Functions:

CREATE FUNCTION dbo.EMP2() RETURNS TABLE as
RETURN (select e.EmployeeID, e.FirstName from Employees e)

Usage:

select * from EMP
select * from dbo.EMP2()

Is there any reason i should use one over the other? views just seem easier to use.

Bata
  • 183
  • 2
  • 6
  • 17
  • You can pass parameters to functions and use them in the function body – squillman Nov 28 '18 at 15:59
  • But wouldn't you be able to get the same behaviour with WHERE conditions? Also thanks for answering the post question. – Bata Nov 28 '18 at 16:00
  • In a simple case like this, yes. But say, for example, you have a `CASE` in the query. Then you could pass parameter values in and use the parameters in the `CASE`. You would have to hardcode those values in a view. – squillman Nov 28 '18 at 16:02
  • A function can contain procedural code. You can use temp variables, loops, if-then-else etc. If you don't need these things, use views. They usually perform better than procedural code. – Olivier Jacot-Descombes Nov 28 '18 at 16:04
  • Thanks guys, appreciate the clarity – Bata Nov 28 '18 at 17:19

0 Answers0