How can I make a Comparaison before Update??

  • Hey Guy please first don't care about my english.

    I need somme explanation of you. I have one row like this in my table Infos.

    Name | Level |

    Stefan | 3 |

    I want to udate level only a newLevel > level

    update Infos set Level =(check (newLevel > level)) where Name = 'Stefan'

    please how can i make a comparaison ??

  • Is this what you are looking for?

    CREATE TABLE #T(AName VARCHAR(10),ALevel INT)

    INSERT INTO #T

    SELECT 'Stefan',3 UNION ALL

    SELECT 'Mike',2 UNION ALL

    SELECT 'Samuel',4

    SELECT AName,ALevel FROM #T -- check input

    DECLARE @newlevel INT

    DECLARE @oldlevel INT

    SET @newlevel = 15

    SET @oldlevel = 3

    UPDATE #T

    SET Alevel = @newlevel WHERE AName LIKE '%Stefan%' AND Alevel = @oldlevel

    SELECT AName,ALevel FROM #T

    Results:

    AName Alevel

    Stefan15

    Mike2

    Samuel4

    A more generalized method:

    DECLARE @newlevel INT

    DECLARE @oldlevel INT

    SET @newlevel = 15

    SET @oldlevel = 3

    UPDATE #T

    SET Alevel = @newlevel WHERE Alevel = @oldlevel

    SELECT AName,ALevel FROM #T --to check results

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]

  • I tunnel it little bittle the same But I want to Compare the Level of Stefan with Level of Samuel and Level of Mike . will i go to do the same as you has done??

  • My apologies, but I do not understand your request. Could you provide a sample of the results you are looking for ?

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]

Viewing 4 posts - 1 through 3 (of 3 total)

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