SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


how to update one column value to another in same table


how to update one column value to another in same table

Author
Message
neethu payal
neethu payal
SSC Veteran
SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)

Group: General Forum Members
Points: 245 Visits: 94
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
Eirikur Eiriksson
Eirikur Eiriksson
SSC-Forever
SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)

Group: General Forum Members
Points: 41548 Visits: 19507
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 (?).
Cool
Eirikur Eiriksson
Eirikur Eiriksson
SSC-Forever
SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)

Group: General Forum Members
Points: 41548 Visits: 19507
Now we have the full question, here is the answer.:-D

Your query is missing the FROM part.
Cool
--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

shubham.saxena67
shubham.saxena67
SSC Veteran
SSC Veteran (276 reputation)SSC Veteran (276 reputation)SSC Veteran (276 reputation)SSC Veteran (276 reputation)SSC Veteran (276 reputation)SSC Veteran (276 reputation)SSC Veteran (276 reputation)SSC Veteran (276 reputation)

Group: General Forum Members
Points: 276 Visits: 228
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..
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search