朋友们,
我正在创建一个临时表。该脚本可能会运行几次,因此我需要检查临时表是否存在,然后将其删除。我已经在下面编写了代码,但是两次运行脚本时出现错误,表明该表已经存在:
数据库中已经有一个名为“#lu_sensor_name_19”的对象 。
IF OBJECT_ID('alarm..#lu_sensor_name_19') IS NOT NULL当Tablle不为null时,似乎不会返回true。我究竟做错了什么?
IF OBJECT_ID('alarm..#lu_sensor_name_19') IS NOT NULL
IF OBJECT_ID('alarm..#lu_sensor_name_19') IS NOT NULL BEGIN DROP TABLE #lu_sensor_name_19 END CREATE TABLE #lu_sensor_name_19( sensorname_id int NOT NULL, sensorname nvarchar(50) NOT NULL, paneltype_id smallint NOT NULL, panel_version_id int NULL, prefix_allowed tinyint NOT NULL, base_allowed tinyint NOT NULL, suffix_allowed tinyint NOT NULL, key_value int NULL, sort_index int NULL, device_allowed tinyint NOT NULL, sensor_name_group_id smallint NOT NULL, )
Temp #Tables是在tempdb中创建的。试试这个:
IF OBJECT_ID('tempdb..#lu_sensor_name_19') IS NOT NULL BEGIN DROP TABLE #lu_sensor_name_19 END CREATE TABLE #lu_sensor_name_19...
SQL Server 2016增加了在一行中完成删除的功能:
DROP TABLE IF EXISTS #lu_sensor_name_19 CREATE TABLE #lu_sensor_name_19...