删除临时表(如果存在)

Ala*_*ord 4 sql sql-server

朋友们,

我正在创建临时表.脚本可能会运行几次,所以我需要检查临时表是否存在然后删除它.我编写了下面的代码但是在运行脚本两次时出现错误,表已经存在:

数据库中已存在名为"#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 
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,
    )
Run Code Online (Sandbox Code Playgroud)

Pau*_*ams 8

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...
Run Code Online (Sandbox Code Playgroud)