sql - SQL Server 临时表重新创建
问题描述
select *
into #t1
from testcur;
if object_id('tempdb..#t1') is not null
drop table #t1;
select *
into #t1
from testcur;
有没有办法再次使用创建临时表?在这种情况下#t1
解决方案
如果你打算在 SSMS (SQL Server Management Studio) 中使用这个脚本,你会得到一个实际上是编译错误的错误:
消息 2714,级别 16,状态 1,第 12 行
数据库中已经有一个名为“#t1”的对象。
SSMS 不明白您已删除表并要再次创建,因此尽管您的查询有效,但它仍会显示错误。
要使 SSMS 对您的脚本感到满意,请在第二次尝试创建表之前使用 GO
select *
into #t1
from testcur;
if object_id('tempdb..#t1') is not null
drop table #t1;
GO -- use GO
select *
into #t1
from testcur;
推荐阅读
- angular - 带有对象代码和描述的角材料自动完成
- sql - 查询返回 N 个随机连接的记录
- server - Graphql 瑜伽服务器未在 Windows 10 上运行
- node.js - 如何在 Node.Js 环境中进行 Paypal 支付
- git - 配置在 WSL 中使用的 Git Bash
- django - 使用 Snowpack 转换 Web 组件并打包依赖项
- javascript - VueJS 有时拖动一个元素会导致它粘在鼠标指针上
- laravel - Breeze auth 中间件重定向回登录页面本身
- mongodb - 带有反应性 MongoDB 的 Spring Boot 抛出打开套接字异常
- reactjs - 如何在 index.html 中添加脚本以响应应用程序