Click here to Skip to main content
15,901,122 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,
I am using SQLServer2008. I created table as Student and inserted some rows,

SID   Sname   SFEE($)  SADDRESS 
01    thniks  2000     chennai,india
o2    madhuri 2100     newdelhi,india
03    sanjeev 1980     devas,india,
 ,     , ,     ,        ,,,,,,,,
 ,     , ,     ,        ,,,,,,,,
 ,     , ,     ,        ,,,,,,,,
1000  dhanu    3000    newyork,USA 


now I want to update SADDRESS of all Students, now i am looking for how to change
SADDRESS (here SADDRESS as different of all Students and also update SADDRESS also ). I am updating each and every row by using update command. It is very time consuming to update each and every row.
Please any one help to me is there another procedure to update that SADDRESS rows at a time.

Thanks in advance,
Thanks and regards
Naresh.
Posted
Updated 2-May-12 2:53am
v3
Comments
VJ Reddy 2-May-12 3:37am    
Please post your further queries in the comment section of the concerned solution or use improve question wizard if you want to improve the question.
I have moved the info posted by you in Solution 2 to comments section of Solution 1.
Thank you.

If you want to update the SADDRESS of particular rows then, WHERE clause can be used like
SQL
UPDATE Student
SET SADDRESS = 'chennai,india' WHERE SID IN (1,2, 10)
GO

here I assumed that SID is of type int and used IN clause. Instead of that any WHERE condition can be used as required.
If SADDRESS in all rows is to be set then
SQL
UPDATE Student
SET SADDRESS = 'chennai,india'
GO

can be used.

[Edit]The following added to update the rows of table from another table [/Edit]

If address A in all rows is to be replaced address B,
address C with address D and like, then a Temporary table can be created and populated with the old address and corresponding new address.
Then the rows can be updated as shown below

SQL
--Create a Temporary table with appropriate data types
CREATE TABLE NewValues (OldAddress NVARCHAR(25), NewAddress NVARCHAR(25))
--Insert old address and the corresponding new address
INSERT INTO NewValues
VALUES  ('chennai,india','mumbai,india'),
	('Add1','Address One'),
        ('Add2', 'Address Two')
--Check whether they are correctly inserted
SELECT * FROM NewValues
--Update the Student table such that for all rows where the 
--SADDRESS is equal to the OldAddress in NewValues (Temporary) table
--replace the SADDRESS in Student with NewAddress from NewValues table 
UPDATE Student
SET Student.SADDRESS  = NewValues.NewAddress
FROM NewValues
INNER JOIN  Student 
ON Student.SADDRESS  = NewValues.OldAddress
--Delete the temporary table
DROP TABLE NewValues
--Check whether replacement was done properly
SELECT * FROM Student
 
Share this answer
 
v3
Comments
VJ Reddy 2-May-12 3:34am    
Moved from solution posted by OP.

thankyou Reddy,
here SADDRESS as not same to SID.
before update each SID Consist of Different SADDRESS as well as
after Updating also SID consist of Different SADDRESS.

for example....
from Student(above) table.

SID=01 consist of "chennai,india" after updating SID=01 consist of "mumbai,india".

like that each student have different SADDRESS to all . now i am using
above update command to update each and every SID.here rows nearly 1000.
so it's take more time to update each and every row. so os there any better way to update all fields.

note: here SID=01,SID=02,SID=03,..... not have same SADDRESS. all are in different address.
please give to me any suggestions .............now i am using case these also very complex ......!
VJ Reddy 2-May-12 3:34am    
One option is as follows
UPDATE Student
SET SADDRESS = 'mumbai,india' WHERE SADDRESS = 'chennai,india'
GO
or use some other where clause such that for all the rows returned by the Where clause the NewAddress is applicable.
by using case in storeprocedure i was solved this
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900