0

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], [TH VWID], [TH KERB], [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.

2
  • 1
    please mention the DBMS that you're using. Lets say, it's sql-server, then tag so. Commented Mar 24, 2011 at 8:11
  • Did one of the answers below solve your question? If so please mark-as-answer so others in the future may benefit from your question. Thanks. Commented Jan 20, 2013 at 19:33

2 Answers 2

3


you can do it like this:
SQL 2000 syntaxis

    IF EXISTS (SELECT 1 FROM sysobjects  WHERE xtype='u' AND name='CVG86_SPE') 
BEGIN
             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], [TH VWID], [TH KERB], [WT PAYLO], [AD GCM], GV
    FROM CVG86_SPE 
END

SQL 2005 and later:

        IF EXISTS ( SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 'CVG86_SPE' )
BEGIN
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], [TH VWID], [TH KERB], [WT PAYLO], [AD GCM], GV
        FROM CVG86_SPE
END
Sign up to request clarification or add additional context in comments.

1 Comment

Why it will fail i have run it on my test DB and runs fine ?
1

Another way to do the same in SQL Server is to use the OBJECT_ID() function:

IF OBJECT_ID('table name', 'U') IS NOT NULL BEGIN
  INSERT ...
  ...
END

The second parameter tells the function to search specifically for the table, not for an arbitrary object, like a stored procedure or a user-defined function.

If you are checking for the existence of a temporary table, use it like this:

IF OBJECT_ID('tempdb..#tmp table name') IS NOT NULL BEGIN
  INSERT ...
  ...
END

(Here the second parameter is unnecessary because from the name of the object it is clear that it can only be a table.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.