Home Forums SQL Server 2005 T-SQL (SS2K5) IF table exists, DROP TABLE then CREATE TABLE - script not working RE: IF table exists, DROP TABLE then CREATE TABLE - script not working

  • A little bit of formatting + fix 😉

    You don't need to include the schema name when you call the OBJECT_ID function:

    OBJECT_ID(N'dbo.mtzcus_2014_CoreMenuAuditDetails', N'U')

    should be just

    OBJECT_ID(N'mtzcus_2014_CoreMenuAuditDetails', N'U')

    IF OBJECT_ID(N'mtzcus_2014_CoreMenuAuditDetails', N'U') IS NOT NULL

    AND EXISTS ( SELECT TOP 1 FROM sys.columns

    WHERE name = N'Threshold2011Or2013Stage1'

    AND [object_id] = OBJECT_ID(N'mtzcus_2014_CoreMenuAuditDetails') )

    BEGIN;

    /* Rename two columns */

    EXECUTE sys.sp_rename

    'dbo.mtzcus_2014_CoreMenuAuditDetails.Threshold2011Or2013Stage1'

    ,'dbo.mtzcus_2014_CoreMenuAuditDetails.Threshold2014Stage1';

    EXECUTE sys.sp_rename

    'dbo.mtzcus_2014_CoreMenuAuditDetails.MetThreshold2011Or2013Stage1'

    ,'dbo.mtzcus_2014_CoreMenuAuditDetails.MetThreshold2014Stage1';

    END;

    ELSE IF OBJECT_ID(N'mtzcus_2014_CoreMenuAuditDetails', N'U') IS NULL

    BEGIN;

    EXECUTE(N'

    CREATE TABLE dbo.mtzcus_2014_CoreMenuAuditDetails

    (

    AuditID UNIQUEIDENTIFIER NOT NULL

    ,MeasureName VARCHAR(100)

    ,CriteriaCertificationNumber VARCHAR(50)

    ,ObjectiveNumber VARCHAR(3)

    ,ObjectiveName VARCHAR(100)

    ,Denominator DECIMAL

    ,Numerator DECIMAL

    ,Percentage DECIMAL(5, 2)

    ,Threshold2014Stage2 DECIMAL(3, 0)

    ,MetThreshold2014Stage2 CHAR(1)

    ,Threshold2014Stage1 DECIMAL(3, 0)

    ,MetThreshold2014Stage1 CHAR(1)

    ,ExclusionDescription VARCHAR(100)

    ,MetExclusion CHAR(1)

    ,SelfAttestation CHAR(1)

    ,CONSTRAINT mtzcus_2014_CoreMenuAuditDetailsFK FOREIGN KEY ( AuditID ) REFERENCES dbo.mtzcus_2014_CoreMenuAuditRun ( AuditID )

    );');

    END

    ---------------------------------------------------------

    It takes a minimal capacity for rational thought to see that the corporate 'free press' is a structurally irrational and biased, and extremely violent, system of elite propaganda.
    David Edwards - Media lens[/url]

    Society has varying and conflicting interests; what is called objectivity is the disguise of one of these interests - that of neutrality. But neutrality is a fiction in an unneutral world. There are victims, there are executioners, and there are bystanders... and the 'objectivity' of the bystander calls for inaction while other heads fall.
    Howard Zinn