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

时间:2014-05-16 19:38:43

标签: sql sql-server-2008 tsql sql-server-2008-r2

我不知道为什么会发生这种情况,我只是在激活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

4 个答案:

答案 0 :(得分:1)

if exists (
select  * from tempdb.dbo.sysobjects o
where o.xtype in ('U')    and o.id = object_id(N'tempdb..#tempTable') ) DROP TABLE #tempTable;

[Check if a temporary table exists and delete if it exists before creating a temporary table

答案 1 :(得分:0)

你可以尝试

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

这可能会给你一些指示,如果有的话,它可能会有冲突。

答案 2 :(得分:0)

这应该更快:

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

答案 3 :(得分: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版本中可用。