dcsimg
Results 1 to 4 of 4

Thread: Updating or Insert Into has me confused

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2006
    Posts
    280

    Updating or Insert Into has me confused

    On a forms first use how do I know If I'm Inserting Into Or Updating the first use? I know there is an easy answer to this but its evading me.

    Thanks

  2. #2
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    6,964

    Re: Updating or Insert Into has me confused

    I'm not sure I follow you. Are you saying you want to update a record if it exists? Otherwise Insert it? If so there are basically three ways of doing it:-
    1. Issue a select statement for the record and either update or insert based on whether you get a result.
    2. Issue an Update then check if any records were affected (e.g. by checking @@Rowcount). If no rows were affected issue an Insert.
    3. Use an Upsert. Different databases have different Syntaxes for this. SQL Server's, for example, is the merge Statement.

    Or are you saying you want to know if you application form is in "Insert" or "Update" mode? In which case you need to track that yourself in the application. We'd need to know a lot more about your application before we could make any suggestions for this.

    Or are you asking something else?
    You can depend upon the Americans to do the right thing. But only after they have exhausted every other possibility - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2006
    Posts
    280

    Re: Updating or Insert Into has me confused

    I BELIEVE THIS IS MY ANSWER
    # 2. Issue an Update then check if any records were affected (e.g. by checking @@Rowcount). If no rows were affected issue an Insert.

    THANKS

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    CT
    Posts
    17,761

    Re: Updating or Insert Into has me confused

    Yes - that is a valid technique

    Code:
    Update AWCUserSalt_T Set PasswordSalt=@PasswordSalt Where UserId=@UserId
    If @@RowCount=0 Insert into AWCUserSalt_T values (@UserId, @PasswordSalt, GetDate())

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width