Specifying schema for temporary tables

问:


I'm used to seeing temporary tables created with just the hash/number symbol, like this:

CREATE TABLE #Test
(
[Id] INT
)

However, I've recently come across stored procedure code that specifies the schema name when creating temporary tables, for example:

CREATE TABLE [dbo].[#Test]
(
[Id] INT
)

Is there any reason why you would want to do this? If you're only specifying the user's default schema, does it make any difference? Does this refer to the [dbo] schema in the local database or the tempdb database?

答:


It won't make any difference if you are specifying the users default schema, but if the users default schema changes then it will try to keep the temporary table in the dbo schema.
Temp tables are created in tempdb so it means you'd need to maintain the schema in tempdb, and offers no benefit.

跟帖:

If you create a schema on tempdb, and map a user to tempdb with that default schema, running CREATE TABLE #test (v INT) will create the table as "dbo.#test". Trying to create a temp table under a schema other than dbo will ignore the schema you speciy and create it in the dbo schema regardless. So CREATE TABLE dummy.#test (v INT) will also create "dbo.#test". --Jim

从上面最后Jim的跟帖中我们可以得知,临时表的schema默认都是位于tempdb数据库下的dbo,如果我们尝试给临时表声明非dbo的schema,例如:CREATE TABLE dummy.#test (v INT),SQL Server会忽略在临时表#test前声明的schema名dummy,创建的还是"dbo.#test",因此给临时表主动声明schema是没有意义的,所有的临时表都会创建在dbo这个系统schema下。

原文链接

最新文章

  1. 利用poi导出Excel
  2. 代码-->发呆
  3. 【Linux】Linux中常用操作命令
  4. UDP SOCKET网络通信 C#
  5. JQuery学习笔记——JQuery基础
  6. struct stat结构体的详解和用法
  7. javascript 获取父页面中元素对象方法
  8. Lua for Windows入门01
  9. Cocos2D:塔防游戏制作之旅(一)
  10. ColorUtil【Color工具类(color整型、rgb数组、16进制互相转换)】
  11. YASnippet - emacs 的代码片段管理工具
  12. kodexplorer免费网盘安装教程
  13. window注册鼠标右键菜单,及子菜单
  14. [20181109]12c sqlplus rowprefetch参数5
  15. 自媒体运营排版利器----Markdown here
  16. eclipse git 分享项目到GitHub上
  17. mapreduce的输入格式 --- InputFormat
  18. VUX
  19. * args 和 **kwargs
  20. sql语句-6-高级查询

热门文章

  1. itext生成pdf如何使用windows系统下的各种字体
  2. 七、Docker启动tocmat 8
  3. 部署LNMP应用平台
  4. Tram POJ - 1847
  5. Linux应急响应
  6. Java多线程编程核心技术-第5章-定时器 Timer-读书笔记
  7. 线程queue、线程进程池,协程
  8. Sublime Text2中的快捷方式及html各种标签(待完善)
  9. Windbg Processes and Threads(进程和线程)窗口的使用
  10. 修改了celery任务老是执行失败,跟shell中调试的结果不同