Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2006
    Posts
    57

    Unanswered: Updating multiple rows with an expression

    OK, I am trying to update a particular column with a numerical number. Here is the query I am using.

    Code:
    UPDATE viewerblock SET dummycat = (?) WHERE dummyproduct = 0
    I am trying to number dummycat row to a certain number for example

    Code:
    dummycat-----------------dummyproduct
    1--------------------------0
    2--------------------------0
    3--------------------------0
    4--------------------------0
    5--------------------------0
    6--------------------------0
    do you see what i am trying to do? I am simply trying to number the dummycat column where ever dummyproduct = 0.

    is this possible to do?

  2. #2
    Join Date
    Mar 2005
    Location
    Netherlands
    Posts
    280
    I think this should do the trick...

    Code:
    DECLARE @i INT
    SET @i = 0
    
    UPDATE viewerblock 
    SET dummycat = @i
       ,@i = @i + 1
    WHERE dummyproduct = 0
    Grtz, Lex

  3. #3
    Join Date
    Sep 2006
    Posts
    57
    thanks alot. I am sure the code above will work but is there a way to do this by not using a stored procedure?

  4. #4
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    Quote Originally Posted by Luke101
    thanks alot. I am sure the code above will work but is there a way to do this by not using a stored procedure?
    the code he gave you does not involve a sp, but why pray tell, would you not want use a stored procedure? I had a developer not long ago argue a case against stored procedures. To make a gym class reference, I pantsed him in that meeting.

    Security, execution plan reuse, the ability to fix the sql without recompiling an application, the ease of updating code when business logic changes etc...
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

Posting Permissions

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