简单的测试台:
USE tempdb;
GO
/*
This DROP TABLE should not be necessary, since the DROP SCHEMA
should drop the table if it is contained within the schema, as
I'd expect it to be.
*/
IF COALESCE(OBJECT_ID('tempdb..#MyTempTable'), 0) <> 0
DROP TABLE #MyTempTable;
IF EXISTS (SELECT 1 FROM sys.schemas s WHERE s.name = 'SomeSchema')
DROP SCHEMA SomeSchema;
GO
CREATE SCHEMA SomeSchema AUTHORIZATION [dbo]
CREATE TABLE SomeSchema.#MyTempTable /* specifying the schema
should not be necesssary since
this statement is executed inside
the context of the CREATE SCHEMA
statement
*/
(
TempTableID INT NOT NULL IDENTITY(1,1)
, SomeData VARCHAR(50) NOT NULL
);
GO
INSERT INTO tempdb.SomeSchema.#MyTempTable (SomeData) VALUES ('This is a test');
SELECT *
FROM tempdb.SomeSchema.#MyTempTable;
GO
SELECT *
FROM sys.objects o
INNER JOIN sys.schemas s ON o.schema_id = s.schema_id
WHERE s.name = 'SomeSchema';
SELECT s.name
, o.name
FROM sys.objects o
INNER JOIN sys.schemas s ON o.schema_id = s.schema_id
WHERE s.name = 'dbo'
AND o.name LIKE '%MyTempTable%';
DROP SCHEMA SomeSchema;
DROP TABLE #MyTempTable;
上面的代码应该#MyTempTable
在tempdb命名模式下创建一个临时表SomeSchema
;但是事实并非如此。而是在dbo
架构中创建表。
这是预期的行为吗?我意识到这肯定是使用特定于架构的临时表的一个极端情况。但是,如果引擎在尝试创建与模式绑定的临时表时提供了错误,或者实际上确实将其绑定到了DDL中指定的模式,那将是很好的选择。
另外,我目前无法访问SQL Server 2014或2016;在这些平台上可以正常工作吗?
针对70-461的培训套件指出“临时表是在dbo模式中的tempdb中创建的。”但是,它所包含的信息并不多,所以我不确定这是否仅意味着当您不这样做的时候”指定模式。
—
Mark Sinkinson '16