SQL Server临时表的正确删除方式(sql server 删除临时表)

2023-02-25,,

sql server临时表的正确删除方式

临时表与一般的表不同,它是保存到tempdb表中。临时表的表名与你所建的表名也不一样,因为他要为不同人的相同操作创建不同的临时表。下文将为您分别示例正确和错误的删除操作,供您参考。

删除sql server临时表和一般表并不相同,下面将为您为别示例错误和正确的删除操作,供您参考,希望对您能够有所帮助。

临时表与一般的表不同,它是保存到tempdb表中。临时表的表名与你所建的表名也不一样,因为他要为不同人的相同操作创建不同的临时表。

1、错误的删除操作:

--错误的临时表删除操作,因为所在数据库不同
if exists (select * from sysobjects where object_id = object_id(n'[dbo].[#temptable]') and type in (n'u'))
begin
drop table [dbo].[temptable]
end
--错误的临时表删除操作,因为临时表名已变
if exists (select * from tempdb.dbo.sysobjects where id = object_id(n'[#temptable]'))
begin
drop table #temptable
end

2、正确的删除方式:

--正确的临时表删除操作
if object_id('tempdb..#temptable') is not null begin
drop table #temptable
end

补充:sql临时表的使用

关系型数据库支持临时表,这是一项很棒的功能。临时表的概念由 sql server 引入,用来存储和处理中间结果。

临时表在会话期间创建,会话结束后自动被删除。临时表可以和普通表一样执行各种操作,比如 select、update、insert、join 等。

mysql 3.23 及其更高版本才支持临时表,如果您使用的 mysql 版本低于 3.23,则不能使用临时表,但可以使用堆表(heap table)。

如前所述,临时表仅存在于会话期间。如果您使用 php 脚本连接数据库,当 php 执行完成后,临时表将被销毁。如果您使用 mysql 客户端连接数据库,当客户端关闭后,临时表将被销毁。

创建临时表的基本语法如下:

create temporary table table_name(
   column1 datatype,
   column2 datatype,
   column3 datatype,
   .....
   columnn datatype,
   primary key( one or more columns )
);

您看,创建临时表的语法和普通表极其相似。临时表创建完成以后,可以使用 insert、delete、update、select 等命令进行增删改查操作。

示例

本例向您展示临时表的用法:

mysql> create temporary table salessummary (
   -> product_name varchar(50) not null
   -> , total_sales decimal(12,2) not null default 0.00
   -> , avg_unit_price decimal(7,2) not null default 0.00
   -> , total_units_sold int unsigned not null default 0
);
query ok, 0 rows affected (0.00 sec)

mysql> insert into salessummary
   -> (product_name, total_sales, avg_unit_price, total_units_sold)
   -> values
   -> ('cucumber', 100.25, 90, 2);

mysql> select * from salessummary;
+--------------+-------------+----------------+------------------+
| product_name | total_sales | avg_unit_price | total_units_sold |
+--------------+-------------+----------------+------------------+
| cucumber     |      100.25 |          90.00 |                2 |
+--------------+-------------+----------------+------------------+
1 row in set (0.00 sec)

当您使用 show tables 命令查看数据库中的表时,临时表将不会被显示。

现在,如果您退出 mysql 会话,然后使用 select 命令查找数据,您将在数据库中找不到任何有效数据,甚至连临时表也不存在。

删除临时表

默认情况下,当数据库连接终止时,mysql 将删除所有的临时表。但是,如果您希望在会话期间删除它们,则可以使用 drop table 命令。

以下是删除临时表的示例:

mysql> create temporary table salessummary (
   -> product_name varchar(50) not null
   -> , total_sales decimal(12,2) not null default 0.00
   -> , avg_unit_price decimal(7,2) not null default 0.00
   -> , total_units_sold int unsigned not null default 0
);
query ok, 0 rows affected (0.00 sec)

mysql> insert into salessummary
   -> (product_name, total_sales, avg_unit_price, total_units_sold)
   -> values
   -> ('cucumber', 100.25, 90, 2);

mysql> select * from salessummary;
+--------------+-------------+----------------+------------------+
| product_name | total_sales | avg_unit_price | total_units_sold |
+--------------+-------------+----------------+------------------+
| cucumber     |      100.25 |          90.00 |                2 |
+--------------+-------------+----------------+------------------+
1 row in set (0.00 sec)
mysql> drop table salessummary;
mysql>  select * from salessummary;
error 1146: table 'tutorials.salessummary' doesn't exist

到此这篇关于sql server临时表的正确删除方式(sql server 删除临时表)的文章就介绍到这了,更多相关sql删除临时表内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!