none
An if condition to check existence of table, if it exists then Truncate the data, if it doesn’t create table

    Question

  • This the command I have written, it does not seem to work, what have I wrong?

     

    IF

    OBJECT

    ('dbo.Reporting_dmt_2','U') is not null

    TRUNCATE

    TABLE

    [dbo].[Reporting_dmt_2]

    ELSE

    CREATE

    TABLE [dbo].[Reporting_dmt_2] (

    [DMT_ID]

    int,

    [groupseqid]

    bigint,

    [seqid]

    bigint,

    [Submitted_Date]

    date,

    [requestor]

    nvarchar(max),

    [analyst]

    nvarchar(128),

    [qa]

    nvarchar(128),

    [hStatus]

    nvarchar(max),

    [Status]

    nvarchar(128),

    [Stat_Time]

    datetime2(3),

    [Stat_Date]

    date,

    [Total_Mins]

    int,

    [SLA_Mins]

    int,

    [SLAMins_D_H_M]

    varchar(28),

    [TotalMins_D_H_M]

    varchar(28),

    [PMT_Tech]

    nvarchar(75),

    [partner_group]

    nvarchar(75),

    [Complete_Date]

    date,

    [QA_Returned_Date]

    date,

    [Service_Name]

    nvarchar(max),

    [Task_Name]

    nvarchar(max),

    [description]

    nvarchar(max),

    [sm]

    nvarchar(128),

    [priority]

    int,

    [tech_name]

    nvarchar(max),

    [SLA]

    nvarchar(50),

    [release_date]

    )

    nvarchar(75)

    GO

    Wednesday, March 12, 2014 5:37 AM

Answers

  • Try this

    IF

    OBJECT_id

    ('dbo.Reporting_dmt_2','U') is not null
    TRUNCATE

    TABLE

    [dbo].[Reporting_dmt_2]
    ELSE

    CREATE

    TABLE [dbo].[Reporting_dmt_2] (
    [DMT_ID]

    int,
    [groupseqid]

    bigint,
    [seqid]

    bigint,
    [Submitted_Date]

    date,
    [requestor]

    nvarchar(max),
    [analyst]

    nvarchar(128),
    [qa]

    nvarchar(128),
    [hStatus]

    nvarchar(max),
    [Status]

    nvarchar(128),
    [Stat_Time]

    datetime2(3),
    [Stat_Date]

    date,
    [Total_Mins]

    int,
    [SLA_Mins]

    int,
    [SLAMins_D_H_M]

    varchar(28),
    [TotalMins_D_H_M]

    varchar(28),
    [PMT_Tech]

    nvarchar(75),
    [partner_group]

    nvarchar(75),
    [Complete_Date]

    date,
    [QA_Returned_Date]

    date,
    [Service_Name]

    nvarchar(max),
    [Task_Name]

    nvarchar(max),
    [description]

    nvarchar(max),
    [sm]

    nvarchar(128),
    [priority]

    int,
    [tech_name]

    nvarchar(max),
    [SLA]

    nvarchar(50),
    [release_date]

    nvarchar(75)
    )
    GO


    Best Regards,Uri Dimant SQL Server MVP, http://sqlblog.com/blogs/uri_dimant/

    MS SQL optimization: MS SQL Development and Optimization
    MS SQL Consulting: Large scale of database and data cleansing
    Remote DBA Services: Improves MS SQL Database Performance
    SQL Server Integration Services: Business Intelligence

    • Marked as answer by delanopg Thursday, March 13, 2014 6:52 PM
    Wednesday, March 12, 2014 6:01 AM