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
 General SQL Server Forums
 New to SQL Server Programming
 Update query for similar values

Author  Topic 

vignesht50
Yak Posting Veteran

82 Posts

Posted - 2013-11-08 : 06:58:59
Hi,

I have a grid like this and when I change Designation value of Name X from PA to PAT, this change should reflect to Sathish Designation too. Changing similar values should reflect all over the grid. Like update database on change of value of similar kind. Any suggestions on update query?

By far I use this query to update the grid. So a where clause should be used now to update the grid to fit this scenario. Correct me if I am wrong.
("UPDATE AppInvent_Test SET Name = @Name, Designation = @Designation, City = @City WHERE EmpID = @EmpID");


EmpID Name Designation City
21 X PA Chn
2 Sathish PA Chn
3 Shiva A Cbe
17 Venkat M Hyd
22 Y SM Cbe
18 Vignesh SA Hyd

Table:
CREATE TABLE [dbo].[AppInvent_Test](
[EmpID] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](100) NULL,
[Designation] [varchar](100) NULL,
[City] [varchar](50) NULL
) ON [PRIMARY]

insert into AppInvent_Test values('X', 'PA','Chn');
insert into AppInvent_Test values('Sathish', 'PA','Chn');
insert into AppInvent_Test values('Shiva', 'A','Cbe');
insert into AppInvent_Test values('Venkat', 'M','Hyd');
insert into AppInvent_Test values('Y', 'SM','Cbe');
insert into AppInvent_Test values('Vignesh', 'SA','Hyd');

GhostX1
Starting Member

6 Posts

Posted - 2013-11-08 : 08:27:08
The following query would change every record that currently has Designation of PA to PAT. Is that what you were after?

Update dbo.AppInvent_Test
Set Designation='PAT'
Where Designation='PA'
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-11-08 : 11:12:58
[code]
UPDATE t
SET Designation = @Designation
FROM AppInvent_Test t
INNER JOIN AppInvent_Test t1
On t1.Designation = t.Designation
AND t1.City = t.City
WHERE t1.EmpID = @EmpID
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

vignesht50
Yak Posting Veteran

82 Posts

Posted - 2013-11-21 : 05:41:07
Thanks visakh, got it.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-11-21 : 07:03:21
cool

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -