View or Temporary Table - which to use in MS SQL Server?

后端 未结 5 1286
礼貌的吻别
礼貌的吻别 2020-12-08 01:55

I have a problem to decide whether to use a view or a temp table.

I have a stored procedure that i call from program. In that SP i store the result of a long query i

5条回答
  •  青春惊慌失措
    2020-12-08 02:46

    If the query is "long" and you are accessing the results from multiple queries, then a temporary table is the better choice.

    A view, in general, is just a short-cut for a select statement. If does not imply that the results are ever run and processed. If you use a view, the results will need to be regenerated each time it is used. Although subsequent runs of the view may be more efficient (say because the pages used by the view query are in cache), a temporary table actually stores the results.

    In SQL Server, you can also use table variables (declare @t table . . .).

    Using a temporary table (or table variable) within a single stored procedure would seem to have few implications in terms of security, simplicity, and column names. Security would be handled by access to the stored procedure. Column names are needed for either solution. Simplicity is hard to judge without more information, but nothing sticks out as being particularly complicated.

提交回复
热议问题