Home Forums SQL Server 2008 SQL Server Newbies To insert data only if it doesnot exist if exist show the table info and print a message RE: To insert data only if it doesnot exist if exist show the table info and print a message

  • 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]