how to update one column value to another in same table

  • my table payment_details structure is

    payment_id payment_code

    1 null

    2 null

    3 null

    4 null

    here payment_id is a primary key and i need to update the whole payment_id column to payment_code column

    so i just tried the below query

    update payment_details

    set payment_code = payment_no

    where payment_code is null

    but it shows subquery error,can anyone say?? loads of thanks in advance

  • neethu payal (6/21/2014)


    my table structure is

    payment_id payment_code

    1 null

    2 null

    3

    4

    I feel there might possibly be something missing in the question, hint (?).

    😎

  • Now we have the full question, here is the answer.:-D

    Your query is missing the FROM part.

    😎

    --my table payment_details structure is

    DECLARE @payment_details TABLE

    (

    payment_id INT PRIMARY KEY CLUSTERED NOT NULL

    ,payment_code INT NULL

    );

    INSERT INTO @payment_details (payment_id)

    VALUES (1),(2),(3),(4)

    UPDATE P

    SET P.payment_code = P.payment_id

    FROM @payment_details P

    WHERE P.payment_code IS NULL

    SELECT

    *

    FROM @payment_details

    Results

    payment_id payment_code

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

    1 1

    2 2

    3 3

    4 4

  • But there is no need to add a from clause as it was working fine.

    See the code below:

    --my table payment_details structure is

    DECLARE @payment_details TABLE

    (

    payment_id INT PRIMARY KEY CLUSTERED NOT NULL

    ,payment_code INT NULL

    );

    INSERT INTO @payment_details (payment_id)

    VALUES (1),(2),(3),(4)

    UPDATE @payment_details

    SET payment_code =payment_id

    WHERE payment_code IS NULL

    SELECT

    *

    FROM @payment_details

    Result:

    payment_id payment_code

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

    1 1

    2 2

    3 3

    4 4

    Kindly, share the exact error..

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

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