What are the benefits of using a view on a temporary table in SQL Server

What are the benefits of using a view over temporary tables. I know you should use the view if the data is reused by other stored procedures, but:

  • Better to use a view than a temporary table?

  • If tables based on constantly updating views look better than a temporary table?

  • If I had to use a where clause for the view, would I be better off using a temporary table?

  • Finally, what are the advantages / disadvantages of using a view or temporary tables?

+3


source to share


3 answers


To find out, ask yourself if you need to reuse information:

  • the view is the glorified SELECT and is mainly used for convenience
  • you can process the view i.e. store it as a table and even index it. See question
  • use a temporary table unless you will be reusing the structure many times, for example in a script that is executed from time to time by the View
  • take up space (especially if they are materialized) and have many views that are difficult to maintain.


Also notice how the temporary tables are destroyed:

  • if you create a temporary table #tbl it will be destroyed when it goes out of scope (like at the end of your script).
  • you can create a temporary table like ## tbl (with two #s) and it will be destroyed when the connection ends.
+10


source


  • In general, yes, since the view is only a stored selection, whereas a temporary table would require some use of tempdb.

  • It doesn't matter in this case. Views are not persisted - they return data from their underlying tables.

  • is also not different.

  • Views are persisted (i.e. the text of the select statement is saved) - they give you a layer above the database that allows you to modify the database without having to use clients using the view.



+4


source


you mix ideas

views are encapsulated logic, nothing more, it helps with code reuse
temporary tables are real, physical storage where data is stored

you cannot compare these two as both have different applications

I don't like using views at all :)

+1


source







All Articles