Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 How can I update multiple rows at the same time?

Author  Topic 

Indra_G
Starting Member

13 Posts

Posted - 2012-05-04 : 22:27:41
I found out I made an error of judgement that the digits that I used for the primary key of my table are too few. I need to add more digits to it. However I want to change the old data so that it reflect the new data. This is how it is:
My previous numbering system for my primary key is: P-000001, P-000002, and so on. It is now approaching P-999999! So I need to add extra digits to it, I planned to add 3 more to P-000XXXXXX. At the same time, we need to retain the old data in the table, but with new format.

I tried to update the old data by using this query:
"Update tblOrder Set Nomor = 'P-000%' Where Nomor = 'P-%'
But it does not want to work. Is there anyway that I can update multiple rows like this?

Thanks.

Software Akuntansi
http://intrikhidupit.blogspot.com/

Vinnie881
Master Smack Fu Yak Hacker

1231 Posts

Posted - 2012-05-04 : 22:54:01

update a
set pk = replace(pk,'p-','p-000') + replace(pk,'p-','')
from
Mytable a


Success is 10% Intelligence, 70% Determination, and 22% Stupidity.
\_/ _/ _/\_/ _/\_/ _/ _/- 881
Go to Top of Page

Indra_G
Starting Member

13 Posts

Posted - 2012-05-05 : 00:26:15
OK, I am sorry, but I am pretty new on this, so please be patient with me. If the table is tblOrder, and the field is Nomor. The query should be:
Update tblOrder set Nomor = replace(Nomor, 'p-', 'p-000') + replace(Nomor, 'p-',"") from Mytable tblOrder
Is that correct? I am sorry I am rather confused with the word Mytable. And, may I know why there are two replace for?

Software Toko|Software Akuntansi|Sistem Informasi Akuntansi |Sistem Informasi Manajemen
Go to Top of Page

Indra_G
Starting Member

13 Posts

Posted - 2012-05-05 : 00:27:55
The database name is Retail

Software Toko|Software Akuntansi|Sistem Informasi Akuntansi |Sistem Informasi Manajemen
Go to Top of Page

Vinnie881
Master Smack Fu Yak Hacker

1231 Posts

Posted - 2012-05-05 : 10:10:14
Update a set Nomor = replace(Nomor, 'p-', 'p-000') + replace(Nomor, 'p-',"") from tblOrder a


Success is 10% Intelligence, 70% Determination, and 22% Stupidity.
\_/ _/ _/\_/ _/\_/ _/ _/- 881
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-05-05 : 10:30:48
quote:
Originally posted by Vinnie881

Update a set Nomor = replace(Nomor, 'p-', 'p-000') + replace(Nomor, 'p-',"") from tblOrder a


Success is 10% Intelligence, 70% Determination, and 22% Stupidity.
\_/ _/ _/\_/ _/\_/ _/ _/- 881

Vinnie, I have the same question that Indra was asking - I didn't follow the reason for the second replace. Wouldn't this be sufficient?
Update tblOrder set Nomor = replace(Nomor, 'p-', 'p-000');
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-05 : 10:45:50
[code]
update a
set pk = stuff(pk,3,0,'000')
from
Mytable a
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Vinnie881
Master Smack Fu Yak Hacker

1231 Posts

Posted - 2012-05-05 : 14:40:15
No need for second replace. It was a mistake that probably is relayed to running on 4 hours of sleep for the last few days.


Success is 10% Intelligence, 70% Determination, and 22% Stupidity.
\_/ _/ _/\_/ _/\_/ _/ _/- 881
Go to Top of Page

Indra_G
Starting Member

13 Posts

Posted - 2012-05-07 : 07:10:33
OK, guys. Thank you so much. It works fine now.

Software Toko|Software Akuntansi|Sistem Informasi Akuntansi |Sistem Informasi Manajemen
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-07 : 17:13:41
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -