2

I don't know why is this happening, I am just checking the syntax with SQLCMD Mode activated, below is my code. I try putting a drop statement before the creation of the temp table, but still is not working

/*
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
DaveGreen
  • 712
  • 6
  • 13
Zinov
  • 3,817
  • 5
  • 36
  • 70
  • 3
    This won't solve your problem but it may help prevent another problem. You need to check if the temp table exists before you call DROP. – Rick S May 16 '14 at 19:42
  • You attempt to create the temp table twice. Is it blowing up on the first attempt or the second? – Ann L. May 16 '14 at 20:45
  • The error comes before running the script just when I am checking the syntax of the script. – Zinov May 20 '14 at 13:40

4 Answers4

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

Community
  • 1
  • 1
Singaravelan
  • 809
  • 3
  • 19
  • 32
0

You could try

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

which may give you some pointers as to what, if anything, it may be conflicting.

Michael Green
  • 1,397
  • 1
  • 17
  • 25
  • The error comes before running the script just when I am checking the syntax of the script. – Zinov May 20 '14 at 13:41
0

This should be faster:

IF OBJECT_ID('tempdb..#tempTable') IS NOT NULL DROP TABLE #tempTable
GO
Enric Naval
  • 164
  • 1
  • 7
0

You can use the older syntax for checking and dropping the table.

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

In the newer version of SQL Server you can use following command:

DROP TABLE IF EXISTS #MC_POZ

IF EXISTS command is available from SQL Server 2016 version.

Emin Mesic
  • 1,681
  • 2
  • 8
  • 18