Insert and update in one stored proc (inserting record twice)

  • From the information you've given, you must have more than one row that meets the criteria defined. It's the only way you would get the problem.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • Chris,Grant..

    Thanks guys I was kind of too sure the statement was right.

    Came in the morning with a fresh brain and fixed it :rolleyes:.

    The app for one reason or the other was updating the stored proc twice 🙂

    Beware of the way you use SqlDataSource.Update 🙁

    Grant Fritchey (11/25/2008)


    From the information you've given, you must have more than one row that meets the criteria defined. It's the only way you would get the problem.

  • I don't know what you're trying to accomplish with these statements.

    The first statement:

    If Exists (Select * From table WHERE a=@a and b=@b)

    update table

    set

    a= @a,

    b=@b

    from table where

    a=@a and b=@b

    This is going to set columns a and b to @a and @b-2 respectively where they already equal to @a and @b-2 respectively. The update doesn't actually do anything.

    The second statement:

    Insert into c

    a,b

    select

    @a,@b

    from table where

    a=@a and b=@b

    will insert @a and @b-2 into table c for each record in table where a=@a and b = @B. There could be 1, 2 or many records that match the criteria.

    The whole thing seems a bit silly to me.

    Todd Fifield

  • tfifield ,

    I know and it was my fault.I didn't think right when i posted it.It was just a quick code sample

    .Its all fixed and all was fine as i posted in my earlier post

    The below could have made more sense 😀

    Tha..

    ---------------------------------

    update table

    set

    products= @products,

    Quantity=@Quantity

    from table where

    orderno=@orderno and p_id=@p_id

    tfifield (11/27/2008)


    I don't know what you're trying to accomplish with these statements.

    The first statement:

    If Exists (Select * From table WHERE a=@a and b=@b)

    update table

    set

    a= @a,

    b=@b

    from table where

    a=@a and b=@b

    This is going to set columns a and b to @a and @b-2 respectively where they already equal to @a and @b-2 respectively. The update doesn't actually do anything.

    The second statement:

    Insert into c

    a,b

    select

    @a,@b

    from table where

    a=@a and b=@b

    will insert @a and @b-2 into table c for each record in table where a=@a and b = @B. There could be 1, 2 or many records that match the criteria.

    The whole thing seems a bit silly to me.

    Todd Fifield

Viewing 4 posts - 16 through 18 (of 18 total)

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