Check if table exists

  • Hi i have a query as below,

    INSERT INTO CarnetMaster.GlassLookupCapacitySpecs

    (ID, CODE, NVIC, RELEASE, DISCON, DRV, TORQUE, KW, BORESTROKE, VINNUMBER, WIDTH, WHEELBASE, SEATS, COMPRATIO, TOWCAP, STEER,

    TURNCIR, HEIGHT, LENGTH, VWIDTH, KERBWT, PAYLOAD, GCM, GVM)

    SELECT NEWID(), CODE, NVIC, RELEASE, DISCON, DRV, TORQUE, KW, [BORE-STROKE], [VIN NUMBER], WIDTH, WHEELBASE, SEA, [TS COMPRAT], [IO TOWC],

    [AP STE], [ER TURNC], [IR HEIG], [HT LENG],

    ,

    , [WT PAYLO], [AD GCM], GV

    FROM CVG86_SPE

    How can i check if table exists,then select from the table CVG86_SPE and Insert. If table do not exists then it should do nothing.

  • You haven't mentioned what version of SQL. If 2005 or above, this will work. If 2000, replace sys.tables with sysobjects and add an extra filter for xtype = 'U'

    IF EXISTS (SELECT 1 FROM sys.tables WHERE name = 'Table to be inserted into'

    BEGIN

    -- do stuff

    END

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply