To insert data only if it doesnot exist if exist show the table info and print a message

  • Basically i am trying to accomplish

    Inserting data into a table only if it doesnot exist and if does to show me the content of the table and print a simple message in either case, but i can seem to accomplish this

    If exists

    (Select * from TableA where PKEY = '1' )

    ---Display the content of the table

    Select * FROM TableA

    Print 'Table A info'

    Else

    Insert Into TableA

    (X,Y,Z)

    Values(1,'testLink', TestValue)

    Print 'Table has been inserted with the specified data'

    GO

    tHanks for the help

  • avi-631555 (9/14/2010)


    Basically i am trying to accomplish

    Inserting data into a table only if it doesnot exist and if does to show me the content of the table and print a simple message in either case, but i can seem to accomplish this

    If exists

    (Select * from TableA where PKEY = '1' )

    ---Display the content of the table

    Select * FROM TableA

    Print 'Table A info'

    Else

    Insert Into TableA

    (X,Y,Z)

    Values(1,'testLink', TestValue)

    Print 'Table has been inserted with the specified data'

    GO

    tHanks for the help

    I guess all that's missing are the BEGIN..END statements to define the IF..ELSE blocks:

    If exists

    (Select * from TableA where PKEY = '1' )

    BEGIN

    ---Display the content of the table

    Select * FROM TableA

    Print 'Table A info'

    END

    Else

    BEGIN

    Insert Into TableA

    (X,Y,Z)

    Values(1,'testLink', TestValue)

    Print 'Table has been inserted with the specified data'

    END



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

  • I get an error with the begin end as well

    Msg 156 level 15 state 1 line 7

    Incorrect syntax near the keyword Else

    Msg 102 level 15 state 1 line 12

    Incorrect syntax near END

    Is there any other way to do this

  • Can you post exactly what you run. The syntax Lutz showed should work.

    It's

    IF ()

    begin

    end

    else

    begin

    end

  • I think i got it the problem was that

    on the else statement where the insert is specified i had to have [] specified for every column instead of simply having () i have no idea why

    but once i added [] the table will update only if the the values are non existent if not the table infor is provided

    If exists

    (Select * from TableA where PKEY = '1' )

    BEGIN

    ---Display the content of the table

    Select * FROM TableA

    Print 'Table A info'

    END

    Else

    BEGIN

    Insert Into TableA

    ([X],[Y],[Z])

    Values(1,'testLink', TestValue)

    Print 'Table has been inserted with the specified data'

    END

    thanks for the help

  • You should still get the inserted message when successful.

    The square bracket issue is weird. Possible any "non-standard" column names? (e.g. including space, comma or any other fancy stuff?)



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

  • no there are no fancy columns, and i do get the inserted message, May be i should show the table info after the insert to show me the insert results. I will let u know what comes out

    thanks

  • Thanks the Merge was really helpful, will use it

    thanks again

Viewing 8 posts - 1 through 7 (of 7 total)

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