首页 > 代码库 > 表变量和临时表

表变量和临时表

SET NOCOUNT ON;

DECLARE @TestTable TABLE (
RowID INT IDENTITY PRIMARY KEY CLUSTERED,
Name VARCHAR(9) NOT NULL UNIQUE,
Age TINYINT NULL,
rowguid UNIQUEIDENTIFIER NOT NULL DEFAULT(newid()));

begin tran

INSERT INTO @TestTable (Name, Age) VALUES (‘Roy‘, 25),(‘中国风‘, 21);
rollback tran

SELECT ‘Insert rows‘ as Action, * FROM @TestTable;
/*
Action RowID Name Age rowguid
Insert rows 1 Roy 25 84C4F6CC-3AB3-4D7D-8779-0DCF3414AB61
Insert rows 2 中国风 21 1DB02A77-F20E-4326-A4E4-023CE9F5DEA7
*/
go
if OBJECT_ID(‘Tempdb..#TestTable‘) is not null
drop table #TestTable
CREATE TABLE #TestTable (
RowID INT IDENTITY PRIMARY KEY CLUSTERED,
Name VARCHAR(9) NOT NULL UNIQUE,
Age TINYINT NULL,
rowguid UNIQUEIDENTIFIER NOT NULL DEFAULT(newid()));

begin tran

INSERT INTO #TestTable (Name,Age) VALUES (‘Roy‘, 25),(‘中国风‘, 21);
rollback tran
SELECT ‘Insert rows‘ as Action,* FROM #TestTable
/*
无记录
*/

表变量和临时表