1、回滚事务对表变量无效,回滚事务对临时表有效
[sql]
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 www.lunwen360 (Name,Age) VALUES ('Roy', 25),('中国风', 21);
rollback tran
SELECT 'Insert rows' as Action,* FROM #TestTable
/*
无记录
*/
2、临时表支持用into生成临时表/TRUNCATE清空表/set identity_insert/drop table,表变量不支持
[sql]
use tempdb
go
if OBJECT_ID('Tempdb..#TestTable') is not null
drop table #TestTable
--into
select 1 as ID into #TestTable
--TRUNCATE
TRUNCATE table #TestTable
go
if OBJECT_ID('Tempdb..#TestTable2') is not null
drop table #TestTable2
Create table #TestTable2(ID int identity,Num int)
go
set identity_insert #TestTable2 on;
insert into #TestTable2(ID,Num) values(1,20)
set identity_insert #TestTable2 off;
go
drop table #TestTable,#TestTable2
3、表变量可以在创建表时定义主健PRIMARY KEY和唯一约束UNIQUE和检查check
[sql]
use tempdb
go
SET NOCOUNT ON;
-- 记录当前tempdb里的表对象ID
if OBJECT_ID('Tempdb..#test') is not null
drop table #test
SELECT object_id
INTO #test
FROM tempdb.sys.tables;
GO
--创建表变量
DECLARE @temp TABLE (
RowID INT NOT NULL,
ColA INT NOT NULL check(ColA<10),
ColB char(1),
ColC char(1),
ColD char(1),
PRIMARY KEY NONCLUSTERED (RowID, ColA),
UNIQUE CLUSTERED (ColB, ColC),
UNIQUE NONCLUSTERED (ColC, ColD));
INSERT INTO @temp VALUES (1, 1, 'B', NULL, 'D');
--获取表变量对象的ID
DECLARE @object_id INT;
SELECT @object_id =
(SELECT object_id
FROM tempdb.sys.tables
EXCEPT
SELECT object_id
FROM #test);
-- truncate的区别获取表变量的索引名
;with IXC
as
(
SELECT IXC.object_id,
IXC.index_id,
IXC.index_column_id,
IXC.is_descending_key,
IXC.is_included_column,
column_name = C.name
FROM tempdb.sys.index_columns IXC
JOIN lumns C
ON IXC.object_id = C.object_id
lumn_id = C.column_id
WHERE IXC.object_id = @object_id
)
select
IX.object_id,
index_name = IX.name,
index_type_desc = IX.type_desc,
IX.is_unique,
IX.is_primary_key,
IX.is_unique_constraint,
IX.is_disabled,
index_columns = STUFF(IXC_COL.index_columns, 1, 2, N'')
from tempdb.sys.indexes as IX
CROSS APPLY(
SELECT index_columns =
(SELECT
N', ' + QUOTENAME(column_name)+ CASE is_descending_key WHEN 1 THEN N'
DESC' ELSE N'' END
DESC' ELSE N'' END
FROM IXC
WHERE object_id = IX.object_id AND index_id = IX.index_id AND is_included_column =
0 ORDER BY index_column_id FOR XML www.lunwen360/a/lunwendaixie/ PATH(''), ROOT('r'), TYPE).value('/r[1]',
'nvarchar(max)')
0 ORDER BY index_column_id FOR XML www.lunwen360/a/lunwendaixie/ PATH(''), ROOT('r'), TYPE).value('/r[1]',
'nvarchar(max)')
)IXC_COL
where IX.object_id= @object_id
/*
object_id index_name index_type_desc is_unique is_primary_key is_unique_constraint
is_disabled index_columns
is_disabled index_columns
1572200651 UQ__#5DB5E0C__D87C70A35F9E293D CLUSTERED 1 0 1 0 [ColB],
[ColC]
[ColC]
1572200651 UQ__#5DB5E0C__287C70A3627A95E8 NONCLUSTERED 1 0 1 0
[ColC], [ColD]
[ColC], [ColD]
1572200651 PK__#5DB5E0C__A5CBEAB465570293 NONCLUSTERED 1 1 0 0
[RowID], [ColA]
[RowID], [ColA]
*/
GO
drop table #test
4、临时表名116个字符,表变量支持128个字符
[sql]
CREATE TABLE #TestTable123456789012345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890 (
12345678901234567890123456789012345678901234567890 (
-- ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
-- 10 20 30 40 50 60 70 80 90 100 110 120
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论