您可以使用永久表、视图和登录触发器创建与 Oracle 样式临时表具有相似行为的内容:
create table dbo._Data (
Session int not null constraint DF__Data DEFAULT (@@SPID),
ColumnA int not null,
ColumnB int not null
)
go
create view dbo.Data (
ColumnA,
ColumnB
)
with schemabinding
as
select
ColumnA,
ColumnB
from
dbo._Data
where
Session = @@SPID
with check option
go
create trigger T_Data_Logon_Cleardown
ON ALL SERVER
FOR LOGON
AS
delete from Database.dbo.Data
go
然后,您对dbo.Data
“表”执行所有操作。
This has the characteristics that each session's rows are isolated, and if you disconnect and reconnect, the "table" will be empty.
What you don't get is an automated clearing of rows on transaction boundaries (if that's your preferred option - you'll have to do a manual delete). Nor is it exactly similar, since rows will remain in the table until a new session claims an old session ID (so cleanup occurs later, if at all). But from the client perspective, it may be "close enough".