Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2003
    Posts
    5

    Unanswered: INSERT or EXECUTE (if, else)

    I am inserting new Holidays in a table in my database


    however, if the holiday date already exists in the system, I will update else I will insert

    lets say columns are date, description and table is Holiday

    then if the date of the holiday to be saved is already present in the Holiday table execute an update else execute an insert

    i want to do this in one sql query

    is it possible ?

  2. #2
    Join Date
    Nov 2003
    Location
    Bangalore, INDIA
    Posts
    333

    Thumbs up Re: INSERT or EXECUTE (if, else)

    Hi,

    It is Possible. U can make use of MERGE command, for this Query.
    SATHISH .

  3. #3
    Join Date
    Nov 2003
    Posts
    5

    example

    could you give an example

  4. #4
    Join Date
    Sep 2003
    Posts
    27
    Here's a quick example of a holidays table with 2 columns:

    MERGE INTO holidays h
    USING (SELECT hol_id, price
    FROM new_holidays) nh
    ON (h.hol_id = nh.hol_id)
    WHEN MATCHED THEN UPDATE SET h.price = nh.price
    WHEN NOT MATCHED THEN INSERT (h.hol_id, h.price)
    VALUES (nh.hol_id, nh.price);

    Similar examples are available in books.
    Or all over the web.

    Tim

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •