Recommend a good tutorial on temp table in SQL Server

I searched, but can't find, a good tutorial on using pace for beginners in SQL Server 2005/2008. I want to know the pros and cons of a temp table compared to a regular table, its lifetime and how the temp table is distributed (in the same session, cross-session)?

thanks in advance George

+3
source share
4 answers

There are two ways to create a temp table. This will create a table and enter data from PHYSICAL;

SELECT FIELD1,FIELD2,FIELD3 INTO TempTable FROM PHYSICALTABLE;

Another way is to use the CREATE TABLE method;

CREATE TABLE #TempTable (ID int,NAME varchar(50));
INSERT INTO #TempTable(ID,NAME) VALUES(1,'PERSON');

Temp DROP TABLE. temp ( ## ), . , , temp UNION, + SUM .

+6

SQL,

-- Create Temp table
CREATE TABLE #temps 
(
    VId int,
    first   VARCHAR( 255 ),
    surname VARCHAR( 255 ),
    DOB DATETIME

    PRIMARY KEY (VId)
)

-- Insert some test data
Insert into #temps (Vid, first, surname, DOB) 
VALUES (1, 'Bob', 'Jennings','23 Feb 1970')


-- Insert some test data
Insert into #temps (Vid, first, surname, DOB) 
VALUES (2, 'John', 'Doe','14 Oct 1965')


-- Select data from the temp table
Select * from #temps


-- Run if you wish to drop the table
-- DROP T ABLE #temps
+2

( ), , ..:

SQL Server Temp? .

Comparing them with "normal" tables, I would say that the biggest differences are essentially that ordinary tables are stored in the database and therefore should be used whenever you need to store the data you work with, since temporary tables should be used, if just working in the context of a request / stored procedure, etc.

+1
source

Source: https://habr.com/ru/post/1710744/


All Articles