USE AdventureWorks2012
GO
-- Create temporary table
CREATE TABLE #T (Col1 varchar(128))
GO
-- Create variable table
CREATE @V TABLE (Col1 varchar(128))
USE AdventureWorks2012
GO
CREATE TABLE #T (Col1 varchar(128))
--Below query lists all the Tables in TempDB
SELECT * From TEMPDB.sys.tables
GO
--Declare a Table Variable
DECLARE @V TABLE(Col1 varchar(128))
--Verify whether Table Variable is created in TempDB
SELECT * From TEMPDB.sys.tables
GO
USE AdventureWorks2012
GO
CREATE TABLE #T (Col1 varchar(128))
INSERT INTO #T SELECT 'A'
BEGIN TRANSACTION
UPDATE #T SET Col1 ='B'
ROLLBACK TRANSACTION
SELECT * FROM #T
DROP TABLE #T
Above scripts, we see that after we roll-back transaction, value of Col1 will return A. Continue with @V
USE AdventureWorks2012
GO
BEGIN TRANSACTION
DECLARE @V table (Col1 varchar(128))
INSERT INTO @V SELECT 'B'
UPDATE @V SET Col1='D'
ROLLBACK TRANSACTION
SELECT * FROM @V
Obviously, value of Col1 is D, we could not roll-back transaction within @V.
USE AdventureWorks2012
GO
DECLARE @Customer TABLE
(
CustomerID INT PRIMARY KEY CLUSTERED,
Name varchar(50) UNIQUE-- CLUSTERED(Name)
)
DECLARE @V TABLE (ID int)
ALTER TABLE @V ADD Col1 varchar(128)
5-@V can be used in User Defined Function, pass variable table to store procedure.
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '@V'.
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near 'Col1'.
USE AdventureWorks2012
GO
CREATE FUNCTION fn_FunctionWithVariableTable()
RETURNS INT
AS
BEGIN
DECLARE @V TABLE (ID int)
IF EXISTS(SELECT 1 FROM @V)
RETURN 1
RETURN 0
END
GO
DROP FUNCTION fn_FunctionWithVariableTable
GO
CREATE FUNCTION fn_FunctionWithTemporaryTable()
RETURNS INT
AS
BEGIN
CREATE TABLE #T(ID int)
IF EXISTS(SELECT 1 FROM #T)
RETURN 1
RETURN 0
END
GO
DROP FUNCTION fn_FunctionWithTemporaryTable
When you create function with #T, error will be raised.
Msg 2772, Level 16, State 1, Procedure fn_FunctionWithTemporaryTable, Line 5
Cannot access temporary
USE AdventureWorks2012
GO
CREATE TABLE #Employee ([BusinessEntityID] INT,[NationalIDNumber] nvarchar(15))
INSERT INTO #Employee
SELECT BusinessEntityID,NationalIDNumber
FROM HumanResources.Employee
CREATE CLUSTERED INDEX idx_BusinessEntityID ON #Employee([BusinessEntityID])
DBCC SHOW_STATISTICS('tempdb..#Employee',idx_BusinessEntityID)
INSERT INTO #Employee
VALUES(291,'A'),(292,'B'),(293,'C')
UPDATE STATISTICS #Employee
DBCC SHOW_STATISTICS('tempdb..#Employee',idx_BusinessEntityID)
Information of statistics changed
SET NOCOUNT ON;
DECLARE @tblEmployee TABLE (
[NationalIDNumber] [nvarchar](15) NOT NULL,
[LoginID] [nvarchar](256) NOT NULL,
[OrganizationNode] [hierarchyid] NULL,
[JobTitle] [nvarchar](50) NOT NULL,
[BirthDate] [date] NOT NULL,
[MaritalStatus] [nchar](1) NOT NULL,
[Gender] [nchar](1) NOT NULL,
[HireDate] [date] NOT NULL,
[SalariedFlag] bit NOT NULL,
[VacationHours] [smallint] NOT NULL,
[SickLeaveHours] [smallint] NOT NULL);
INSERT INTO @tblEmployee
SELECT [NationalIDNumber]
,[LoginID]
,[OrganizationNode]
,[JobTitle]
,[BirthDate]
,[MaritalStatus]
,[Gender]
,[HireDate]
,[SalariedFlag]
,[VacationHours]
,[SickLeaveHours]
FROM AdventureWorks2012.[HumanResources].[Employee];
SET STATISTICS PROFILE ON;
SELECT * FROM @tblEmployee
SET STATISTICS PROFILE OFF;
SELECT * FROM @tblEmployee OPTION(RECOMPILE)
USE AdventureWorks2012
GO
CREATE TABLE #Sales ([SalesOrderID] INT,[ProductID] INT,[ModifiedDate] DATE)
INSERT INTO #Sales
SELECT SalesOrderID,ProductID,ModifiedDate
FROM Sales.SalesOrderDetail
SELECT *
FROM #Sales p
ORDER BY [ProductID] ASC
OPTION( MAXDOP 2)
DROP TABLE #Sales
USE AdventureWorks2012
GO
DECLARE @Sales TABLE ([SalesOrderID] INT,[ProductID] INT,[ModifiedDate] DATE)
INSERT INTO @Sales
SELECT SalesOrderID,ProductID,ModifiedDate
FROM Sales.SalesOrderDetail
SELECT *
FROM @Sales p
ORDER BY [ProductID] ASC
OPTION( MAXDOP 2)
USE AdventureWorks2012
GO
CREATE TABLE #T (Col1 varchar(128))
INSERT INTO #T SELECT 'A'
GO
SELECT * FROM #T
GO
DECLARE @V table (Col1 varchar(128))
INSERT INTO @V SELECT 'B'
GO
SELECT * FROM @V
After GO, we could still query data from #T but error will happen if we query @V
I have just provide to you some differences between #T and @V. In generall, you could use both to store a small data. However, you must consider if your data is large ( about >10.000 rows) because #T has properties to help run better such indexes, parallelism mechanism, statistics,...
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
Msg 1087, Level 15, State 2, Line 1
Must declare the table variable "@V".
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (1)
Commented:
>English is not my native language.
You're one of the better 'English as a Second Language' authors compared to many we see around here..