Temporary tables are created in the TempDB database and can be classified into Local and Global:
- Local Temporary Tables are created with the prefix "#" and have restricted visibility for the connection responsible for their creation; other connections do not "see" the table.
- Global Temporary Tables are created with the prefix "##" and are visible by all connections
Syntax:
CREATE TABLE #Tablename
(
fieldnameA varchar(80),
fieldnameB money
)
Temporary tables are widely used when we need to gather several records from several tables in a single selection and display them in any application (eg Delphi, Visual FoxPro, Visual Studio, ASP.NET, etc.)
Check if a temp table exists and delete if it exists before creating a temp table
IF OBJECT_ID('tempdb..#Tablename') IS NOT NULL DROP TABLE #Results
GO
CREATE TABLE #TableName ( test CHAR(3))
GO
SELECT * from #TableName
GO
Comments
Post a Comment