Define a Temp Table
Define a Temp Table
A temporary table, often referred to as a "temp table," is a type of table in SQL that is used to store data temporarily during the execution of a query or a series of queries. Temp tables are particularly useful for intermediate storage of data that is needed only for the duration of a session or transaction. Here are some key points about temp tables:
Scope and Lifespan:
#
). They are visible only within the session that created them and are automatically dropped when the session ends.##
). They are visible to all sessions and are dropped when the session that created them ends and the last active session referencing them is closed.Creation and Usage:
CREATE TABLE
statement with the appropriate prefix (#
or ##
).INSERT INTO
or SELECT INTO
statements.SELECT
, INSERT
, UPDATE
, and DELETE
.Performance:
tempdb
database, which is optimized for temporary storage and operations.Use Cases:
-- Create a local temporary table
CREATE TABLE #TempStudents (
StudentID INT PRIMARY KEY,
FirstName NVARCHAR(50),
LastName NVARCHAR(50),
Age INT
);
-- Insert data into the temp table
INSERT INTO #TempStudents (StudentID, FirstName, LastName, Age)
VALUES (1, 'John', 'Doe', 20);
-- Select data from the temp table
SELECT * FROM #TempStudents;
-- Drop the temp table
DROP TABLE #TempStudents;
**Adv...
entry
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào