数据库中已有一个名为“#TempTable”的对象

问题描述 投票:0回答:5

我不知道为什么会发生这种情况,我只是在激活 SQLCMD 模式的情况下检查语法,下面是我的代码。我尝试在创建临时表之前放置一个 drop 语句,但仍然不起作用

/*
Deployment script for XXXX

This code was generated by a tool.
Changes to this file may cause incorrect behavior and will be lost if
the code is regenerated.
*/

GO
SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, CONCAT_NULL_YIELDS_NULL, QUOTED_IDENTIFIER ON;

SET NUMERIC_ROUNDABORT OFF;


GO
:setvar DatabaseName "XXXX"
:setvar DefaultFilePrefix "XXXX"
:setvar DefaultDataPath "E:\MSSQL\DATA\"
:setvar DefaultLogPath "E:\MSSQL\DATA\"

GO
:on error exit
GO
/*
Detect SQLCMD mode and disable script execution if SQLCMD mode is not supported.
To re-enable the script after enabling SQLCMD mode, execute the following:
SET NOEXEC OFF; 
*/
:setvar __IsSqlCmdEnabled "True"
GO
IF N'$(__IsSqlCmdEnabled)' NOT LIKE N'True'
    BEGIN
        PRINT N'SQLCMD mode must be enabled to successfully execute this script.';
        SET NOEXEC ON;
    END


GO
USE [$(DatabaseName)];


GO
/*
 Pre-Deployment Script Template                         
--------------------------------------------------------------------------------------
 This file contains SQL statements that will be executed before the build script.   
 Use SQLCMD syntax to include a file in the pre-deployment script.          
 Example:      :r .\myfile.sql                              
 Use SQLCMD syntax to reference a variable in the pre-deployment script.        
 Example:      :setvar TableName MyTable                            
               SELECT * FROM [$(TableName)]                 
--------------------------------------------------------------------------------------
*/
GO

GO
:setvar path ".\Sprint XXXX"
/*
Post-Deployment Script Template                         
--------------------------------------------------------------------------------------
 This file contains SQL statements that will be appended to the build script.       
 Use SQLCMD syntax to include a file in the post-deployment script.         
 Example:      :r .\myfile.sql                              
 Use SQLCMD syntax to reference a variable in the post-deployment script.       
 Example:      :setvar TableName MyTable                            
               SELECT * FROM [$(TableName)]                 
--------------------------------------------------------------------------------------
*/

DECLARE @MC_Products_Id int;
DECLARE @MC_EffectiveDate datetime;
DECLARE @MC_Now datetime;
DECLARE @MC_User nvarchar(30);
DECLARE @MC_Zone_Name nvarchar(64);


SET @MC_User = SUSER_NAME();
SET @MC_Now = GETUTCDATE();


SET @MC_EffectiveDate = '2014-03-01';
SET @MC_Zone_Name = 'Zone X';



SELECT 
    @MC_Products_Id = id
    FROM [dbo].[Products]
    WHERE name = 'X'


DROP TABLE #MC_POZ;
-- Create temporary table to hold new items
CREATE TABLE #MC_POZ (
    option_id int
    , product_id int
    , zone_id int
    , name nvarchar(50)
    , effective_from datetime
    , effective_thru datetime NULL
    , updated_by nvarchar(30)
    , updated_on datetime
    , Version int NULL
)
;

INSERT INTO #MC_POZ
    SELECT (SELECT id FROM [dbo].[Options] WHERE [name] = 'I'), @MC_Products_Id, Z.id, 'I', @MC_EffectiveDate, null, @MC_User, @MC_Now, null FROM [dbo].[Zones] AS Z WHERE Z.[name] = @MC_Zone_Name
    UNION ALL SELECT (SELECT id FROM [dbo].[Options] WHERE [name] = 'II'), @MC_Products_Id, Z.id, 'II', @MC_EffectiveDate, null, @MC_User, @MC_Now, null FROM [dbo].[Zones] AS Z WHERE Z.[name] = @MC_Zone_Name
    UNION ALL SELECT (SELECT id FROM [dbo].[Options] WHERE [name] = 'III'), @MC_Products_Id, Z.id, 'III', @MC_EffectiveDate, null, @MC_User, @MC_Now, null FROM [dbo].[Zones] AS Z WHERE Z.[name] = @MC_Zone_Name
    UNION ALL SELECT (SELECT id FROM [dbo].[Options] WHERE [name] = 'IV'), @MC_Products_Id, Z.id, 'IV', @MC_EffectiveDate, null, @MC_User, @MC_Now, null FROM [dbo].[Zones] AS Z WHERE Z.[name] = @MC_Zone_Name
    UNION ALL SELECT (SELECT id FROM [dbo].[Options] WHERE [name] = 'V'), @MC_Products_Id, Z.id, 'V', @MC_EffectiveDate, null, @MC_User, @MC_Now, null FROM [dbo].[Zones] AS Z WHERE Z.[name] = @MC_Zone_Name
;


MERGE
    INTO [dbo].[Product_Options_zone] AS T
    USING #MC_POZ AS S
    ON (T.product_id = S.product_id) AND (T.option_id = S.option_id) AND (T.zone_id = S.zone_id)
    WHEN MATCHED THEN
        UPDATE SET
            [name] = S.[name]
            , [effective_from] = S.effective_from
            , [effective_thru] = S.effective_thru
            , [updated_by] = S.updated_by
            , [updated_on] = S.updated_on
            , [Version] = S.Version
    WHEN NOT MATCHED THEN
        INSERT (option_id, product_id, zone_id, name, effective_from, effective_thru, updated_by, updated_on, Version)
        VALUES(option_id, product_id, zone_id, name, effective_from, effective_thru, updated_by, updated_on, Version)
;
-- Delete temporary table
DROP TABLE #MC_POZ;
SELECT * FROM [dbo].[Product_Options_zone] AS POZ WHERE POZ.product_id = @MC_Products_Id;

-- Set the user again for another insertion
SET @MC_User = SUSER_NAME();
SET @MC_Now = GETUTCDATE();

-- Setup product characteristics
SET @MC_EffectiveDate = '2014-03-01';
SET @MC_Zone_Name = 'Zone XX';


SELECT 
    @MC_Products_Id = id
    FROM [dbo].[Products]
    WHERE name = 'XX'


DROP TABLE #MC_POZ;
-- Create temporary table to hold new items
CREATE TABLE #MC_POZ (
    option_id int
    , product_id int
    , zone_id int
    , name nvarchar(50)
    , effective_from datetime
    , effective_thru datetime NULL
    , updated_by nvarchar(30)
    , updated_on datetime
    , Version int NULL
)
;

INSERT INTO #MC_POZ
    SELECT (SELECT id FROM [dbo].[Options] WHERE [name] = 'I'), @MC_Products_Id, Z.id, 'I', @MC_EffectiveDate, null, @MC_User, @MC_Now, null FROM [dbo].[Zones] AS Z WHERE Z.[name] = @MC_Zone_Name
    UNION ALL SELECT (SELECT id FROM [dbo].[Options] WHERE [name] = 'II'), @MC_Products_Id, Z.id, 'II', @MC_EffectiveDate, null, @MC_User, @MC_Now, null FROM [dbo].[Zones] AS Z WHERE Z.[name] = @MC_Zone_Name
    UNION ALL SELECT (SELECT id FROM [dbo].[Options] WHERE [name] = 'III'), @MC_Products_Id, Z.id, 'III', @MC_EffectiveDate, null, @MC_User, @MC_Now, null FROM [dbo].[Zones] AS Z WHERE Z.[name] = @MC_Zone_Name
    UNION ALL SELECT (SELECT id FROM [dbo].[Options] WHERE [name] = 'IV'), @MC_Products_Id, Z.id, 'IV', @MC_EffectiveDate, null, @MC_User, @MC_Now, null FROM [dbo].[Zones] AS Z WHERE Z.[name] = @MC_Zone_Name
    UNION ALL SELECT (SELECT id FROM [dbo].[Options] WHERE [name] = 'V'), @MC_Products_Id, Z.id, 'V', @MC_EffectiveDate, null, @MC_User, @MC_Now, null FROM [dbo].[Zones] AS Z WHERE Z.[name] = @MC_Zone_Name
;


MERGE
    INTO [dbo].[Product_Options_zone] AS T
    USING #MC_POZ AS S
    ON (T.product_id = S.product_id) AND (T.option_id = S.option_id) AND (T.zone_id = S.zone_id)
    WHEN MATCHED THEN
        UPDATE SET
            [name] = S.[name]
            , [effective_from] = S.effective_from
            , [effective_thru] = S.effective_thru
            , [updated_by] = S.updated_by
            , [updated_on] = S.updated_on
            , [Version] = S.Version
    WHEN NOT MATCHED THEN
        INSERT (option_id, product_id, zone_id, name, effective_from, effective_thru, updated_by, updated_on, Version)
        VALUES(option_id, product_id, zone_id, name, effective_from, effective_thru, updated_by, updated_on, Version)
;
-- Delete temporary table
DROP TABLE #MC_POZ;
SELECT * FROM [dbo].[Product_Options_zone] AS POZ WHERE POZ.product_id = @MC_Products_Id;




GO

GO
PRINT N'Update complete.';


GO
sql sql-server-2008 t-sql sql-server-2008-r2
5个回答
2
投票
if exists (
select  * from tempdb.dbo.sysobjects o
where o.xtype in ('U')    and o.id = object_id(N'tempdb..#tempTable') ) DROP TABLE #tempTable;

[创建临时表前先检查临时表是否存在,如果存在则删除


0
投票

你可以试试

select * from tempdb.sys.objects where name like '#%' and type = 'U';

这可能会给您一些提示,告诉您哪些内容(如果有的话)可能存在冲突。


0
投票

这应该更快:

IF OBJECT_ID('tempdb..#tempTable') IS NOT NULL DROP TABLE #tempTable
GO

0
投票

您可以使用旧语法来检查和删除表。

IF OBJECT_ID('tempdb..#MC_POZ') IS NOT NULL 
    DROP TABLE #MC_POZ

在较新版本的 SQL Server 中,您可以使用以下命令:

DROP TABLE IF EXISTS #MC_POZ

IF EXISTS
命令从 SQL Server 2016 版本开始可用。


0
投票

当我发现删除的表的名称(带有

DROP TABLE IF EXISTS ...
)和创建的表的名称并不相同时,即使它们乍一看非常相似,我也遇到了这个错误。

#mylongtmptablename
这样的东西可能必须是
#mylongtablename
来代替。

© www.soinside.com 2019 - 2024. All rights reserved.